Skip to content

Commit 06af7db

Browse files
committed
working app just needs hooking up
1 parent b65fdc3 commit 06af7db

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

lib/math_demo/circles.rb

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
require_relative 'circumcircle'
4+
require_relative 't_points'
5+
require_relative 'triangle_point'
6+
7+
class Circles < Propane::App
8+
9+
def settings
10+
size(800, 600, P2D)
11+
end
12+
13+
def setup
14+
sketch_title 'Circles'
15+
color_mode(HSB, 360, 100, 100, 100)
16+
reset
17+
ellipse_mode(RADIUS)
18+
end
19+
20+
def draw
21+
fill(0, 0, 0)
22+
no_stroke
23+
reset if (frame_count % 8_000).zero?
24+
@points.each do |point|
25+
# change direction sometimes
26+
point.direction Vec2D.random if rand > 0.96
27+
point.update
28+
end
29+
30+
# set the style of the circle
31+
@dc = map1d(millis, 0..150_000, 0..360) # slowly changes hue
32+
stroke((@c + @dc) % 360, 50, 100, 5)
33+
no_fill
34+
35+
## verifies if there is a circle and draw it
36+
draw_circle @points unless @points.collinear?
37+
end
38+
39+
def draw_circle(pts)
40+
circumcircle = Circumcircle.new(@points.positions)
41+
circumcircle.calculate
42+
center_point = circumcircle.center
43+
radius = circumcircle.radius
44+
ellipse(center_point.x, center_point.y, radius, radius)
45+
end
46+
47+
def reset
48+
@c = rand(360)
49+
@points = TrianglePoints.new
50+
3.times { @points << TPoint.new(Vec2D.new(rand(5..width - 5), rand(5..height - 5))) }
51+
background 0
52+
end
53+
end
54+
55+
Circles.new

lib/math_demo/triangle_points.rb lib/math_demo/t_points.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ def <<(pt)
2020
end
2121

2222
def collinear?
23-
full? ? (points[0] - points[1]).cross(points[1] - points[2]).zero? : false
23+
full? ? (positions[0] - positions[1]).cross(positions[1] - positions[2]).zero? : false
24+
end
25+
26+
# returns positions as an array of Vec2D
27+
def positions
28+
points.map(&:pos)
2429
end
2530

2631
def full?

lib/math_demo/triangle_point.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
# particle and triangle point
3+
class TPoint
4+
include Propane::Proxy
5+
attr_reader :pos, :vel, :accel, :xbound, :ybound
6+
# attr_reader :width, :height # uncomment for testing
7+
8+
def initialize(position)
9+
@pos = position
10+
@vel = Vec2D.new
11+
@accel = Vec2D.random
12+
@xbound = Boundary.new(0, width)
13+
@ybound = Boundary.new(0, height)
14+
end
15+
16+
def direction(acc)
17+
# direction of the acceleration is defined by the new angle
18+
@accel = acc
19+
# magnitude of the acceleration is proportional to the angle between
20+
# acceleration and velocity
21+
dif = acc.angle_between(vel)
22+
dif = map1d(dif, 0..PI, 0.1..0.001)
23+
@accel = acc * dif
24+
end
25+
26+
def update
27+
@vel += accel
28+
@vel.set_mag(1.5) { vel.mag > 1.5 }
29+
@pos += vel
30+
check_bounds
31+
end
32+
33+
private
34+
35+
def check_bounds
36+
@vel.x *= -1 if xbound.exclude? pos.x
37+
@vel.y *= -1 if ybound.exclude? pos.y
38+
end
39+
end
40+
41+
# we are looking for excluded values
42+
Boundary = Struct.new(:lower, :upper) do
43+
def exclude?(val)
44+
true unless (lower...upper).cover? val
45+
end
46+
end

test/triangle_points_test.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
require_relative './test_helper'
2-
require_relative '../lib/math_demo/triangle_points'
2+
require_relative '../lib/math_demo/t_points'
3+
require_relative '../lib/math_demo/triangle_point'
34

45
METHODS = %i(each map size shift clear [] collinear? full?).freeze
56

67
class TrianglePointsTest < Minitest::Test
78
def setup
89
@tpoints = TrianglePoints.new
9-
@tpoints << Vec2D.new(0, 0)
10-
@tpoints << Vec2D.new(100, 100)
11-
@tpoints << Vec2D.new(300, 300)
10+
@tpoints << TPoint.new(Vec2D.new(0, 0))
11+
@tpoints << TPoint.new(Vec2D.new(100, 100))
12+
@tpoints << TPoint.new(Vec2D.new(300, 300))
1213
end
1314

1415
def test_respond

0 commit comments

Comments
 (0)