1   package fi.jyu.mit.graphics;
2   
3   import java.awt.Graphics;
4   
5   /**
6    * Luokka R²->R funktion kuvaajan piirtämiseksi.
7    * @author Vesa Lappalainen
8    *
9    */
10  public class FunctionMapR2R  extends BasicShape {
11      private double xmin,ymin,xmax,ymax;
12      private int nx,ny;
13      private FunctionR2R f;
14      
15      /**
16       * Luo uuden R²->R funktion kuvaajan
17       * @param f piirrettävä funktio
18       * @param xmin x minimi
19       * @param ymin y minimi
20       * @param xmax x maksimi
21       * @param ymax y maksimi
22       * @param nx piirrettävien x-pisteiden määrä
23       * @param ny piirrettävien y-pisteiden määrä
24       */
25      public FunctionMapR2R(FunctionR2R f, double xmin, double ymin, double xmax, double ymax,int nx, int ny) {
26          super();
27          this.f = f;
28          this.xmin = xmin;
29          this.ymin = ymin;
30          this.xmax = xmax;
31          this.ymax = ymax;
32          this.nx = nx;
33          this.ny = ny;
34      }
35  
36      /**
37       * Luo uuden R²->R funktion kuvaajan
38       * @param f piirrettävä funktio
39       * @param xmin x minimi
40       * @param ymin y minimi
41       * @param xmax x maksimi
42       * @param ymax y maksimi
43       */
44      public FunctionMapR2R(FunctionR2R f, double xmin, double ymin, double xmax, double ymax) {
45          this(f,xmin,ymin,xmax,ymax,40,40);      
46      }
47      
48      protected void drawShape(Graphics g, Matrix a) {
49          double dx = (xmax-xmin)/nx;
50          double dy = (ymax-ymin)/ny;
51          double x,x2=xmax-dx/2;
52          double y,y2=ymax-dy/2;
53          SPoint sp = new SPoint(0,0);
54          Vector vr = new Vector(); // NOPMD luulee vääräksi vectoriksi
55  
56          y = ymin;
57          while ( y < y2 ) {
58            xDir(g,a,y,vr,sp);
59            y = y + dy;
60          }
61          xDir(g,a,ymax,vr,sp);
62  
63          x = xmin;
64          while ( x < x2 ) {
65            yDir(g,a,x,vr,sp);
66            x = x + dx;
67          }
68          yDir(g,a,xmax,vr,sp);
69      }
70      
71      /**
72       * This is only for inner purposes
73       * @param g
74       * @param a
75       * @param y
76       * @param vr
77       * @param sp
78       */ 
79      private void xDir(Graphics g,Matrix a, double y, Vector vr, SPoint sp) { // NOPMD luulee vääräksi vektoriksi
80          double x = xmin;
81          double dx = (xmax-xmin)/nx;
82          int x1,y1,x2,y2;
83          a.transform(vr.set(x,y,f.f(x,y)),sp);
84          x1 = sp.getX();
85          y1 = sp.getY();
86  
87          x += dx;
88          while ( x < xmax ) { 
89              a.transform(vr.set(x,y,f.f(x,y)),sp);
90              x2 = sp.getX();
91              y2 = sp.getY();
92              g.drawLine(x1,y1,x2,y2);
93              x1 = x2; y1 = y2;
94              x += dx;
95          }    
96          x = xmax;
97          a.transform(vr.set(x,y,f.f(x,y)),sp);
98          x2 = sp.getX();
99          y2 = sp.getY();
100         g.drawLine(x1,y1,x2,y2);
101     }     
102 
103     /**
104      * This is only for inner purposes
105      * @param g
106      * @param a
107      * @param x
108      * @param vr
109      * @param sp
110      */
111     private void yDir(Graphics g,Matrix a, double x, Vector vr, SPoint sp) { // NOPMD Vector
112         double y = ymin;
113         double dy = (ymax-ymin)/ny;
114         int x1,y1,x2,y2;
115         a.transform(vr.set(x,y,f.f(x,y)),sp);
116         x1 = sp.getX();
117         y1 = sp.getY();
118 
119         y += dy;
120         while ( y < ymax ) { 
121             a.transform(vr.set(x,y,f.f(x,y)),sp);
122             x2 = sp.getX();
123             y2 = sp.getY();
124             g.drawLine(x1,y1,x2,y2);
125             x1 = x2; y1 = y2;
126             y += dy;
127         }    
128         y = ymax;
129         a.transform(vr.set(x,y,f.f(x,y)),sp);
130         x2 = sp.getX();
131         y2 = sp.getY();
132         g.drawLine(x1,y1,x2,y2);
133     }     
134 }
135