CHAPTER 7

Coordinate Calculations

We have seen, in this chapter, how Pascal standard functions and functions that you write yourself can be used to clarify, simplify, and shorten programs that carry out calculations.


- Independent and Dependent Variables:

7.1 Square: using dependent variables

prog6_1.jpg

  1. void setup(){
  2. size(800,600);
  3. }
  4.  
  5. void draw(){
  6.  
  7. //independent variables
  8. int x1 = 400;
  9. int y1 = 200;
  10. int side = 300;
  11.  
  12. //dependent variables
  13. int x2 = x1 + side;
  14. int y2 = y1 + side;
  15.  
  16. line(x1,y1,x2,y1);
  17. line(x2,y1,x2,y2);
  18. line(x2,y2,x1,y2);
  19. line(x1,y2,x1,y1);
  20. }

- Rounding and Truncating:

7.2 Triangle

prog7_2.jpg

  1. void setup(){
  2. size(800,600);
  3. }
  4.  
  5. float radians = 0.01745;
  6.  
  7. //independent variables
  8. int x1 = 300;
  9. int y1 = 500;
  10. int side = 400;
  11.  
  12. //dependent variables
  13. int x2 = x1 + side;
  14. int x3 = (x1 + x2)/2;
  15. int height = int(side * sin(60 * radians));
  16. int y2 = y1 - height;
  17. //it is a subtraction because the origin is at the bottom of the workspace
  18.  
  19. void draw(){
  20. line(x1,y1,x2,y1);
  21. line(x2,y1,x3,y2);
  22. line(x3,y2,x1,y1);
  23. }

- Writing your Own Functions:

7.3 Volume

Output: 27

  1. void setup(){
  2. size(800,600);
  3. }
  4.  
  5. void draw(){
  6. println(cube(3));
  7. }
  8.  
  9. int cube(int x){
  10. int cube = x * x * x;
  11. return cube;
  12. }

7.4 Volume

Answer:


- Functions With Several Parameters:

7.5 Nest Triangles

prog7_5.png

  1. void setup(){
  2. size(800,600);
  3. }
  4.  
  5. void draw(){
  6.  
  7. //set coordinates of outer triangle
  8. int x1 = 100;
  9. int y1 = 200;
  10. int x2 = 400;
  11. int y2 = 500;
  12. int x3 = 500;
  13. int y3 = 300;
  14.  
  15. //calculate midpoints of sides
  16. int x12 = midpoint(x1, x2);
  17. int y12 = midpoint(y1, y2);
  18. int x23 = midpoint(x2, x3);
  19. int y23 = midpoint(y2, y3);
  20. int x31 = midpoint(x3, x1);
  21. int y31 = midpoint(y3, y1);
  22.  
  23. //draw outer triangle
  24. line(x1,y1,x2,y2);
  25. line(x2,y2,x3,y3);
  26. line(x3,y3,x1,y1);
  27.  
  28. //draw inner triangle connecting midpoints
  29. line(x12,y12,x23,y23);
  30. line(x23,y23,x31,y31);
  31. line(x31,y31,x12,y12);
  32. }
  33.  
  34. //desired fuction
  35. int midpoint(int high, int low){
  36. int midpoint = (low + high)/2;
  37. return midpoint;
  38. }

- Local Variables:

7.6 Line

prog7_6.png

  1. void setup(){
  2. size(800,600);
  3. }
  4.  
  5. void draw(){
  6.  
  7. //set variables defining line
  8. int angle = 45;
  9. int x1 = 300;
  10. int y1 = 200;
  11. int lenght1 = 400;
  12.  
  13. //calculate dependent variables
  14. float theta = radians(angle);
  15. int x = x2(x1, lenght1, theta);
  16. int y = y2(y1, lenght1, theta);
  17.  
  18. //draw line
  19. line(x1,y1,x,y);
  20. }
  21.  
  22. int x2(int x1, int length1, float theta){
  23.  
  24. //calculate real value for base
  25. float base = length1 * cos(theta);
  26.  
  27. //round and add to X1
  28. int x2 = x1 + round(base);
  29. return x2;
  30. }
  31.  
  32. int y2(int y1, int length1, float theta){
  33.  
  34. //calculate real value for base
  35. float height1 = length1 * sin(theta);
  36.  
  37. //round and add to X1
  38. int y2 = y1 + round(height1);
  39. return y2;
  40. }

- Functions to Map between Screen Coordinate Systems:

Comments

Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.