Advertisement
Guest User

VitasLALALA

a guest
Oct 1st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. PFont f;
  2. void setup()
  3. {
  4.   size(800, 600);
  5.   frame.setResizable(true);
  6.   f = createFont("Arial",10,true);
  7. }
  8.  
  9. void draw()
  10. {
  11.   float x1 = -10, x2 = 15, y0;
  12.   float lx, ly, miny, maxy;
  13.   float coef;
  14.   float xsep = width, ysep = height;
  15.  
  16.   miny = maxy = func(x1);
  17.   for (int i = int(x1); i <= x2; i++)
  18.   {
  19.     float y = func(i);
  20.     if (miny > y) miny = y;
  21.     if (maxy < y) maxy = y;
  22.   }
  23.  
  24.  
  25.   if (abs(x1) + abs(x2) > abs(miny) + abs(func(maxy)))
  26.     coef = width/(abs(x1) + abs(x2));
  27.   else
  28.     coef = height/(abs(miny) + abs(maxy));
  29.  
  30.   if (x1 < 0) xsep = width * abs(x1/(abs(x1) + abs(x2)));
  31.   else if (x2 < 0) xsep = width * abs(x1/(abs(x1) + abs(x2)));
  32.   if (miny < 0) ysep = height * abs(miny/(abs(miny) + abs(maxy)));
  33.   else if (maxy < 0) ysep = height * abs(miny/(abs(miny) + abs(maxy)));
  34.  
  35.   line(xsep, 0, xsep, height);
  36.   line(0, ysep, width, ysep);
  37.  
  38.   textFont(f);  
  39.   textAlign(LEFT);
  40.   text(int(x1), xsep + int(x1)*coef, ysep-0.2*coef);  
  41.   textAlign(CENTER);
  42.   for (int i = int(x1) + 1; i < x2; i++)
  43.   {
  44.      text(i, xsep + i*coef, ysep-0.2*coef);
  45.   }
  46.   textAlign(RIGHT);
  47.   text(int(x2), xsep + int(x2)*coef, ysep-0.2*coef);  
  48.  
  49.  
  50.   textAlign(LEFT, CENTER);
  51.   text(round(miny), xsep+0.2*coef, ysep - round(miny)*coef);  
  52.   text(round(maxy), xsep+0.2*coef, ysep - round(maxy)*coef);
  53.  
  54.   for (int i = round(miny) + 1; i < round(maxy); i++)
  55.   {
  56.     text(round(i), xsep+0.2*coef, ysep - round(i)*coef);  
  57.   }
  58.  
  59.   do
  60.   {
  61.    y0 = func(x1);
  62.    ly = y0;
  63.    lx = x1;
  64.    x1 += 1/coef;
  65.    y0 = func(x1);
  66.    line(xsep + coef * lx, ysep - coef*ly, xsep + coef*x1, ysep - coef*y0);
  67.   }
  68.   while (x1 <= x2);
  69. }
  70.  
  71. float func(float x)
  72. {
  73.   return cos(x);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement