Spline.java 2.6 KB

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