|
| 1 | +### Dealing with processing coordinate system ### |
| 2 | + |
| 3 | +For library see `library/circles/circles.rb` |
| 4 | + |
| 5 | +If you want to create Math Sketches in processing, you need to deal with peculiar coordinate systems, where the Y-axis is inverted in theory you should be able to do. |
| 6 | + |
| 7 | +```ruby |
| 8 | +scale(1, -1) |
| 9 | +translate(0, -height) |
| 10 | +``` |
| 11 | +But that will mess up any text (plus you probably need to `push_matrix` and `pop_matrix`) so it is probably simpler to create a parallel coordinate system for the math, and translate that back to the screen (using the processing `map` function or in `propane` and `JRubyArt` use `map1d`). |
| 12 | + |
| 13 | +We have done this in `circumcircle_sketch.rb` or just accept the processing coordinate system as we have with `basic_cirmcumcircle_sketch.rb` (it is much simpler). |
| 14 | + |
| 15 | +### PVector limitations ### |
| 16 | + |
| 17 | +PVector is a 3D vector, that is often used as a 2D vector, to my mind that is just plain wrong. If you evaluate the cross product of a 2D vector you get a float (you may see somewhere that a cross product does not exist for 2D vectors, but it can be useful). The cross product of PVector yields another PVector, so cannot be used in the calculation of the area of the triangle as defined by two vectors _cf_ Vec2D:- |
| 18 | + |
| 19 | +```ruby |
| 20 | +a = Vec2D.new(100, 0) |
| 21 | +b = Vec2D.new(0, 100) |
| 22 | + |
| 23 | +a.cross(b).abs == 10_000 # or twice the area of the triangle enclosed by a, b |
| 24 | + |
| 25 | +``` |
| 26 | +Further we can use the cross product in a test for collinearity |
| 27 | + |
| 28 | +```ruby |
| 29 | + |
| 30 | +# given 3 points in 2D space |
| 31 | +a = Vec2D.new(0, 0) |
| 32 | +b = Vec2D.new(100, 100) |
| 33 | +c = Vec2D.new(200, 200) |
| 34 | + |
| 35 | +(a - b).cross(b - c) == 0 # the area of the triangle is zero, so a, b, c are collinear |
| 36 | + |
| 37 | +``` |
| 38 | + |
| 39 | +Also because we were able to separate the logic, we were able to confidently re-factor the Barbara Almeida sketch to use Matrix math to determine the circumcenter |
| 40 | + |
| 41 | +### Matrix Math ### |
| 42 | + |
| 43 | +For detailed workings see [Circumcircle at Mathworld Wolfram.com][circumcircle] |
| 44 | + |
| 45 | + |
| 46 | +a = {{x<sub>1</sub> y<sub>1</sub> 1}, {x<sub>2</sub> y<sub>2</sub> 1}, {x<sub>3</sub> y<sub>3</sub> 1}} |
| 47 | + |
| 48 | +bx = -{{x<sub>1</sub><sup>2</sup> + y<sub>1</sub><sup>2</sup> y<sub>1</sub> 1}, {x<sub>2</sub><sup>2</sup> + y<sub>2</sub><sup>2</sup> y<sub>2</sub> 1}, {x<sub>3</sub><sup>2</sup> + y<sub>3</sub><sup>2</sup> y<sub>3</sub> 1}} |
| 49 | + |
| 50 | +by = {{x<sub>1</sub><sup>2</sup> + y<sub>1</sub><sup>2</sup> x<sub>1</sub> 1}, {x<sub>2</sub><sup>2</sup> + y<sub>2</sub><sup>2</sup> x<sub>2</sub> 1}, {x<sub>3</sub><sup>2</sup> + y<sub>3</sub><sup>2</sup> x<sub>3</sub> 1}} |
| 51 | + |
| 52 | +xo = -bx / 2 * a |
| 53 | + |
| 54 | +yo = -by / 2 * a |
| 55 | + |
| 56 | + |
| 57 | +[circumcircle]:http://mathworld.wolfram.com/Circumcircle.html |
| 58 | + |
| 59 | + |
| 60 | + |
0 commit comments