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