Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PFont f;
- void setup()
- {
- size(800, 600);
- frame.setResizable(true);
- f = createFont("Arial",10,true);
- }
- void draw()
- {
- float x1 = -10, x2 = 15, y0;
- float lx, ly, miny, maxy;
- float coef;
- float xsep = width, ysep = height;
- miny = maxy = func(x1);
- for (int i = int(x1); i <= x2; i++)
- {
- float y = func(i);
- if (miny > y) miny = y;
- if (maxy < y) maxy = y;
- }
- if (abs(x1) + abs(x2) > abs(miny) + abs(func(maxy)))
- coef = width/(abs(x1) + abs(x2));
- else
- coef = height/(abs(miny) + abs(maxy));
- if (x1 < 0) xsep = width * abs(x1/(abs(x1) + abs(x2)));
- else if (x2 < 0) xsep = width * abs(x1/(abs(x1) + abs(x2)));
- if (miny < 0) ysep = height * abs(miny/(abs(miny) + abs(maxy)));
- else if (maxy < 0) ysep = height * abs(miny/(abs(miny) + abs(maxy)));
- line(xsep, 0, xsep, height);
- line(0, ysep, width, ysep);
- textFont(f);
- textAlign(LEFT);
- text(int(x1), xsep + int(x1)*coef, ysep-0.2*coef);
- textAlign(CENTER);
- for (int i = int(x1) + 1; i < x2; i++)
- {
- text(i, xsep + i*coef, ysep-0.2*coef);
- }
- textAlign(RIGHT);
- text(int(x2), xsep + int(x2)*coef, ysep-0.2*coef);
- textAlign(LEFT, CENTER);
- text(round(miny), xsep+0.2*coef, ysep - round(miny)*coef);
- text(round(maxy), xsep+0.2*coef, ysep - round(maxy)*coef);
- for (int i = round(miny) + 1; i < round(maxy); i++)
- {
- text(round(i), xsep+0.2*coef, ysep - round(i)*coef);
- }
- do
- {
- y0 = func(x1);
- ly = y0;
- lx = x1;
- x1 += 1/coef;
- y0 = func(x1);
- line(xsep + coef * lx, ysep - coef*ly, xsep + coef*x1, ysep - coef*y0);
- }
- while (x1 <= x2);
- }
- float func(float x)
- {
- return cos(x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement