|
| 1 | +# Loosely based on a sketch by Bárbara Almeida |
| 2 | +# https://www.openprocessing.org/sketch/179567 |
| 3 | +# Here is a sketch that clearly demonstrates some of the most egregious parts of |
| 4 | +# vanilla processing:- |
| 5 | +# PVector is over complicated and overloads 2D and 3D functionality, cf Vec2D |
| 6 | +# JRubyArt and propane which behaves as a 2D vector (cross product is a float) |
| 7 | +# and can easily be used in chain calculations. |
| 8 | + |
| 9 | +################################################## |
| 10 | +# Usage click on screen with mouse to generate a point, repeat as required |
| 11 | +# to create a triangle, and the circumcircle will be drawn unless the points are |
| 12 | +# collinear. Additional clicks erase the first point and add a new point. To |
| 13 | +# demonstrate the stupid processing coordinate convention press 'c' or 'C', you |
| 14 | +# will see the graph is inverted and origin y is top left, also demonstrates |
| 15 | +# the collinearity test press space-bar to clear |
| 16 | +################################################## |
| 17 | + |
| 18 | +load_library :simple_circle # but does not use math_point |
| 19 | + |
| 20 | +attr_reader :pnt, :points, :circle, :renderer |
| 21 | + |
| 22 | +def settings |
| 23 | + size 800, 800, P2D |
| 24 | + # pixel_density(2) # for HiDpi screens |
| 25 | + # smooth # see https://processing.org/reference/smooth_.html |
| 26 | +end |
| 27 | + |
| 28 | +def setup |
| 29 | + sketch_title 'Simplified Circumcircle Sketch' |
| 30 | + @pnt = Vec2D.new |
| 31 | + @points = SimplePoints.new |
| 32 | + ellipse_mode RADIUS |
| 33 | + @renderer = AppRender.new(self) |
| 34 | +end |
| 35 | + |
| 36 | +def draw |
| 37 | + graph_paper |
| 38 | + stroke_weight 4 |
| 39 | + stroke 200, 0, 0 |
| 40 | + point(pnt.x, pnt.y) |
| 41 | + points.map { |pt| point(pt.x, pt.y) } |
| 42 | + return unless points.full? |
| 43 | + return render_triangle if points.collinear? # Renders a straight line |
| 44 | + circle = Circumcircle.new(points) |
| 45 | + circle.calculate |
| 46 | + center = circle.center |
| 47 | + point(center.x, center.y) |
| 48 | + no_fill |
| 49 | + stroke_weight 2 |
| 50 | + render_triangle |
| 51 | + stroke 0, 0, 255, 100 |
| 52 | + ellipse(center.x, center.y, circle.radius, circle.radius) |
| 53 | +end |
| 54 | + |
| 55 | +def mouse_pressed |
| 56 | + points << Vec2D.new(mouse_x, mouse_y) |
| 57 | +end |
| 58 | + |
| 59 | +def render_triangle |
| 60 | + stroke 255, 255, 0, 100 |
| 61 | + begin_shape |
| 62 | + points.each do |point| |
| 63 | + point.to_vertex(renderer) |
| 64 | + end |
| 65 | + end_shape(CLOSE) |
| 66 | +end |
| 67 | + |
| 68 | +def key_pressed |
| 69 | + case key |
| 70 | + when ' ' |
| 71 | + points.clear |
| 72 | + when 'c', 'C' |
| 73 | + (0..400).step(200) do |i| |
| 74 | + points << Vec2D.new(i, i) |
| 75 | + end |
| 76 | + puts 'collinear points' if points.collinear? |
| 77 | + end |
| 78 | +end |
| 79 | + |
| 80 | +def graph_paper |
| 81 | + background(180) |
| 82 | + cell_size = 20 # size of the grid |
| 83 | + (0..width).step(cell_size) do |i| |
| 84 | + stroke(75, 135, 155) |
| 85 | + line(i, 0, i, height) |
| 86 | + end |
| 87 | + (0..height).step(cell_size) do |i| |
| 88 | + line(0, i, width, i) |
| 89 | + end |
| 90 | +end |
0 commit comments