Skip to content

Commit 25501fa

Browse files
committed
mass refactor
1 parent 0016f6e commit 25501fa

File tree

7 files changed

+177
-20
lines changed

7 files changed

+177
-20
lines changed

contributed/circles.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
load_library :circle
2+
3+
def settings
4+
size(800, 600, P2D)
5+
end
6+
7+
def setup
8+
sketch_title 'Circles'
9+
color_mode(HSB, 360, 100, 100, 100)
10+
reset
11+
ellipse_mode(RADIUS)
12+
end
13+
14+
def draw
15+
fill(0, 0, 0)
16+
no_stroke
17+
reset if (frame_count % 8_000).zero?
18+
@points.each do |point|
19+
# change direction sometimes
20+
point.direction Vec2D.random if rand > 0.96
21+
point.update
22+
end
23+
24+
# set the style of the circle
25+
@dc = map1d(millis, 0..150_000, 0..360) # slowly changes hue
26+
stroke((@c + @dc) % 360, 50, 100, 5)
27+
no_fill
28+
29+
## verifies if there is a circle and draw it
30+
draw_circle @points unless @points.collinear?
31+
end
32+
33+
def draw_circle(pts)
34+
circumcircle = Circumcircle.new(@points.positions)
35+
circumcircle.calculate
36+
center_point = circumcircle.center
37+
radius = circumcircle.radius
38+
ellipse(center_point.x, center_point.y, radius, radius)
39+
end
40+
41+
def reset
42+
@c = rand(360)
43+
@points = TrianglePoints.new
44+
3.times { @points << TPoint.new(Vec2D.new(rand(5..width - 5), rand(5..height - 5))) }
45+
background 0
46+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require_relative 'lib/circumcircle'
2+
require_relative 'lib/triangle_point'
3+
require_relative 'lib/t_points'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
require 'matrix'
3+
4+
# Circumcircle from 3 points
5+
class Circumcircle
6+
attr_reader :center, :radius, :points
7+
def initialize(points)
8+
@points = points
9+
end
10+
11+
def calculate
12+
@center = Vec2D.new(-(bx / am), -(by / am))
13+
@radius = center.dist(points[2]) # points[2] = c
14+
end
15+
16+
private
17+
18+
# Matrix math see matrix_math.md and in detail
19+
# http://mathworld.wolfram.com/Circumcircle.html
20+
21+
def am
22+
2 * Matrix[
23+
*points.map { |pt| [pt.x, pt.y, 1] }
24+
].determinant
25+
end
26+
27+
def bx
28+
-Matrix[
29+
*points.map { |pt| [pt.x * pt.x + pt.y * pt.y, pt.y, 1] }
30+
].determinant
31+
end
32+
33+
def by
34+
Matrix[
35+
*points.map { |pt| [pt.x * pt.x + pt.y * pt.y, pt.x, 1] }
36+
].determinant
37+
end
38+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
require 'forwardable'
3+
MAX_POINT = 3
4+
# A collection of a maximum of 3 points in the processing world
5+
# includes a collinearity test using Vec2D
6+
class TrianglePoints
7+
extend Forwardable
8+
def_delegators(:@points, :each, :map, :size, :shift, :clear, :[])
9+
include Enumerable
10+
11+
attr_reader :points
12+
13+
def initialize
14+
@points = []
15+
end
16+
17+
def <<(pt)
18+
points << pt
19+
shift if size > MAX_POINT
20+
end
21+
22+
def collinear?
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)
29+
end
30+
31+
def full?
32+
points.length == MAX_POINT
33+
end
34+
end
Lines changed: 46 additions & 0 deletions
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 Processing::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

processing_app/library/vecmath/vec2d/library/circle/lib/circumcircle.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Circumcircle from 3 points
1+
# frozen_string_literal: true
22
require 'matrix'
33

4+
# Circumcircle from 3 points
45
class Circumcircle
56
attr_reader :center, :radius, :points
67
def initialize(points)
@@ -19,25 +20,19 @@ def calculate
1920

2021
def am
2122
2 * Matrix[
22-
[points[0].x, points[0].y, 1],
23-
[points[1].x, points[1].y, 1],
24-
[points[2].x, points[2].y, 1]
23+
*points.map { |pt| [pt.x, pt.y, 1] }
2524
].determinant
2625
end
2726

2827
def bx
2928
-Matrix[
30-
[points[0].x * points[0].x + points[0].y * points[0].y, points[0].y, 1],
31-
[points[1].x * points[1].x + points[1].y * points[1].y, points[1].y, 1],
32-
[points[2].x * points[2].x + points[2].y * points[2].y, points[2].y, 1]
29+
*points.map { |pt| [pt.x * pt.x + pt.y * pt.y, pt.y, 1] }
3330
].determinant
3431
end
3532

3633
def by
3734
Matrix[
38-
[points[0].x * points[0].x + points[0].y * points[0].y, points[0].x, 1],
39-
[points[1].x * points[1].x + points[1].y * points[1].y, points[1].x, 1],
40-
[points[2].x * points[2].x + points[2].y * points[2].y, points[2].x, 1]
35+
*points.map { |pt| [pt.x * pt.x + pt.y * pt.y, pt.x, 1] }
4136
].determinant
4237
end
4338
end

processing_app/library/vecmath/vec2d/library/simple_circle/lib/circumcircle.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Circumcircle from 3 points
1+
# frozen_string_literal: true
22
require 'matrix'
33

4+
# Circumcircle from 3 points
45
class Circumcircle
56
attr_reader :center, :radius, :points
67
def initialize(points)
@@ -19,25 +20,19 @@ def calculate
1920

2021
def am
2122
2 * Matrix[
22-
[points[0].x, points[0].y, 1],
23-
[points[1].x, points[1].y, 1],
24-
[points[2].x, points[2].y, 1]
23+
*points.map { |pt| [pt.x, pt.y, 1] }
2524
].determinant
2625
end
2726

2827
def bx
2928
-Matrix[
30-
[points[0].x * points[0].x + points[0].y * points[0].y, points[0].y, 1],
31-
[points[1].x * points[1].x + points[1].y * points[1].y, points[1].y, 1],
32-
[points[2].x * points[2].x + points[2].y * points[2].y, points[2].y, 1]
29+
*points.map { |pt| [pt.x * pt.x + pt.y * pt.y, pt.y, 1] }
3330
].determinant
3431
end
3532

3633
def by
3734
Matrix[
38-
[points[0].x * points[0].x + points[0].y * points[0].y, points[0].x, 1],
39-
[points[1].x * points[1].x + points[1].y * points[1].y, points[1].x, 1],
40-
[points[2].x * points[2].x + points[2].y * points[2].y, points[2].x, 1]
35+
*points.map { |pt| [pt.x * pt.x + pt.y * pt.y, pt.x, 1] }
4136
].determinant
4237
end
4338
end

0 commit comments

Comments
 (0)