|
1 /*************************************************************************** |
|
2 Title: GraphBrowser/Spline.java |
|
3 ID: $Id$ |
|
4 Author: Stefan Berghofer, TU Muenchen |
|
5 Copyright 1997 TU Muenchen |
|
6 |
|
7 This class is used for drawing spline curves (which are not yet |
|
8 supported by the Java AWT). |
|
9 ***************************************************************************/ |
|
10 |
|
11 package GraphBrowser; |
|
12 |
|
13 import java.awt.*; |
|
14 import java.util.*; |
|
15 import java.io.*; |
|
16 |
|
17 class SplineSection { |
|
18 |
|
19 /*** Section of a spline function ***/ |
|
20 |
|
21 double x_b,x_c,x_d; |
|
22 double y_b,y_c,y_d; |
|
23 int dx,dy; |
|
24 |
|
25 public SplineSection(double xb,double xc,double xd, |
|
26 double yb,double yc,double yd,int dx2,int dy2) { |
|
27 x_b=xb;x_c=xc;x_d=xd; |
|
28 y_b=yb;y_c=yc;y_d=yd; |
|
29 dx=dx2;dy=dy2; |
|
30 } |
|
31 |
|
32 public Point draw(Graphics g,Point s) { |
|
33 double m; |
|
34 int s_x,s_y,e_x=0,e_y=0; |
|
35 int x,y; |
|
36 |
|
37 s_x=s.x;s_y=s.y; |
|
38 if (dx>=dy) { |
|
39 if (dx==0) return s; |
|
40 m=1/((double)dx); |
|
41 for (x=0;x<dx;x++) { |
|
42 e_x=(int)(Math.round((x_b*x*m+x_c)*x*m+x_d)); |
|
43 e_y=(int)(Math.round((y_b*x*m+y_c)*x*m+y_d)); |
|
44 g.drawLine(s_x,s_y,e_x,e_y); |
|
45 s_x=e_x;s_y=e_y; |
|
46 } |
|
47 } else { |
|
48 m=1/((double)dy); |
|
49 for (y=0;y<dy;y++) { |
|
50 e_x=(int)(Math.round((x_b*y*m+x_c)*y*m+x_d)); |
|
51 e_y=(int)(Math.round((y_b*y*m+y_c)*y*m+y_d)); |
|
52 g.drawLine(s_x,s_y,e_x,e_y); |
|
53 s_x=e_x;s_y=e_y; |
|
54 } |
|
55 } |
|
56 return new Point(e_x,e_y); |
|
57 } |
|
58 } |
|
59 |
|
60 public class Spline { |
|
61 |
|
62 Vector sections; |
|
63 Vector points; |
|
64 Point start,end; |
|
65 |
|
66 public Spline(Vector pts) { |
|
67 int i; |
|
68 double d0,d1,d2,d3; |
|
69 Point p0,p1,p2; |
|
70 SplineSection s; |
|
71 |
|
72 start=(Point)(pts.firstElement()); |
|
73 end=(Point)(pts.lastElement()); |
|
74 |
|
75 sections=new Vector(10,10); |
|
76 for (i=1;i<=pts.size()-4;i+=3) { |
|
77 p0=(Point)(pts.elementAt(i)); |
|
78 p1=(Point)(pts.elementAt(i+1)); |
|
79 p2=(Point)(pts.elementAt(i+2)); |
|
80 s=new SplineSection( |
|
81 (double)(p2.x-2*p1.x+p0.x), |
|
82 2.0*(p1.x-p0.x), |
|
83 (double)(p0.x), |
|
84 |
|
85 (double)(p2.y-2*p1.y+p0.y), |
|
86 2.0*(p1.y-p0.y), |
|
87 (double)(p0.y), |
|
88 |
|
89 Math.abs(p2.x-p0.x), |
|
90 Math.abs(p2.y-p0.y) |
|
91 ); |
|
92 |
|
93 sections.addElement(s); |
|
94 } |
|
95 points=pts; |
|
96 } |
|
97 |
|
98 public void draw(Graphics g) { |
|
99 Enumeration e1=sections.elements(); |
|
100 Point p=start; |
|
101 |
|
102 while (e1.hasMoreElements()) |
|
103 p=((SplineSection)(e1.nextElement())).draw(g,p); |
|
104 g.drawLine(p.x,p.y,end.x,end.y); |
|
105 } |
|
106 |
|
107 public void PS(PrintStream p) { |
|
108 Point p0,p1,p2; |
|
109 int i; |
|
110 |
|
111 p.println("n "+start.x+" "+start.y+" m"); |
|
112 for (i=1;i<=points.size()-4;i+=3) { |
|
113 p0=(Point)(points.elementAt(i)); |
|
114 p1=(Point)(points.elementAt(i+1)); |
|
115 p2=(Point)(points.elementAt(i+2)); |
|
116 p.println(p0.x+" "+p0.y+" l"); |
|
117 p.println(p0.x+" "+p0.y+" "+p1.x+" "+p1.y+" "+p2.x+" "+p2.y+" c"); |
|
118 } |
|
119 p.println(end.x+" "+end.y+" l s"); |
|
120 } |
|
121 } |