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