]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/xdot/Shapes.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / internal / xdot / Shapes.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.graphviz.internal.xdot;
13
14 import java.awt.geom.Ellipse2D;
15 import java.awt.geom.Path2D;
16
17 public class Shapes {
18         public static Path2D createPolyline(double[] p) {
19                 Path2D path = new Path2D.Double();
20                 path.moveTo(p[0], -p[1]);
21                 for(int i=2;i<p.length;i+=2)
22                         path.lineTo(p[i], -p[i+1]);
23                 return path;
24         }
25         
26         public static Path2D createPolygon(double[] p) {
27                 Path2D path = createPolyline(p);
28                 path.closePath();
29                 return path;
30         }
31         
32         public static Path2D createBSpline(double[] p) {
33                 Path2D.Double path = new Path2D.Double();
34                 path.moveTo(p[0], p[1]);
35                 double cp2x = 0.5 * (p[2] + p[4]);
36                 double cp2y = 0.5 * (p[3] + p[5]);
37                 double newCp1x = (2.0/3.0) * p[4] + (1.0/3.0) * p[6];
38                 double newCp1y = (2.0/3.0) * p[5] + (1.0/3.0) * p[7];
39                 path.curveTo(
40                                 p[2], p[3],
41                                 cp2x, cp2y,
42                                 0.5*(cp2x + newCp1x), 0.5*(cp2y + newCp1y)                              
43                                 );
44                 for(int i=4;i<p.length-8;i+=2) {
45                         double cp1x = newCp1x;
46                         double cp1y = newCp1y;
47                         cp2x = (1.0/3.0) * p[i] + (2.0/3.0) * p[i+2];
48                         cp2y = (1.0/3.0) * p[i+1] + (2.0/3.0) * p[i+3];
49                         newCp1x = (2.0/3.0) * p[i+2] + (1.0/3.0) * p[i+4];
50                         newCp1y = (2.0/3.0) * p[i+3] + (1.0/3.0) * p[i+5];
51                         path.curveTo(
52                                         cp1x, cp1y,
53                                         cp2x, cp2y,
54                                         0.5*(cp2x + newCp1x), 0.5*(cp2y + newCp1y)                              
55                                         );      
56                 }       
57                 {
58                         double cp1x = newCp1x;
59                         double cp1y = newCp1y;
60                         cp2x = (1.0/3.0) * p[p.length-8] + (2.0/3.0) * p[p.length-6];
61                         cp2y = (1.0/3.0) * p[p.length-7] + (2.0/3.0) * p[p.length-5];
62                         newCp1x = 0.5 * (p[p.length-6] + p[p.length-4]);
63                         newCp1y = 0.5 * (p[p.length-5] + p[p.length-3]);
64                         path.curveTo(
65                                         cp1x, cp1y,
66                                         cp2x, cp2y,
67                                         0.5*(cp2x + newCp1x), 0.5*(cp2y + newCp1y)                              
68                                         );
69                         path.curveTo(
70                                         newCp1x, newCp1y,
71                                         p[p.length-4], p[p.length-3],
72                                         p[p.length-2], p[p.length-1]                            
73                                         );
74                 }
75                 return path;
76         }
77         
78         public static Path2D createCubicSegments(double[] p) {
79                 Path2D.Double path = new Path2D.Double();
80                 path.moveTo(p[0], -p[1]);
81                 for(int i=2;i<p.length;i+=6) {
82                         path.curveTo(p[i],-p[i+1],p[i+2],-p[i+3],p[i+4],-p[i+5]);
83                 }
84                 return path;
85         }
86         
87         public static Ellipse2D createEllipse(double x, double y, double w, double h) {
88                 return new Ellipse2D.Double(x-w, -y-h, w*2.0, h*2.0);
89         }
90 }