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