Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ require 'rake/testtask'

desc 'Test'
task :test do
sh 'jruby test/pbisector_test.rb'
sh 'jruby test/circumcircle_test.rb'
sh 'jruby test/triangle_points_test.rb'
end
45 changes: 26 additions & 19 deletions lib/math_demo/circumcircle.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Circumcircle from 3 points
require_relative './pbisector'
require 'matrix'

class Circumcircle
attr_reader :center, :radius, :points
Expand All @@ -8,29 +8,36 @@ def initialize(points)
end

def calculate
ab = PBisector.new(points[0], points[1]) # find 2 midpoints
bc = PBisector.new(points[1], points[2])
@center = circumcenter(ab, bc)
@center = Vec2D.new(-(bx / am), -(by / am))
@radius = center.dist(points[2]) # points[2] = c
end

private

Vect = Struct.new(:x, :y, :z)
# Matrix math see matrix_math.md and in detail
# http://mathworld.wolfram.com/Circumcircle.html

def circumcenter(pb1, pb2)
# equation of the first bisector (ax - y = -b)
a0 = Math.tan pb1.angle
v0 = pb1.midpoint
a1 = Math.tan pb2.angle
v1 = pb2.midpoint
eq0 = Vect.new(a0, -1, -1 * (v0.y - v0.x * a0))
eq1 = Vect.new(a1, -1, -1 * (v1.y - v1.x * a1))
# calculate x and y coordinates of the circumcenter
ox = (eq1.y * eq0.z - eq0.y * eq1.z) /
(eq0.x * eq1.y - eq1.x * eq0.y)
oy = (eq0.x * eq1.z - eq1.x * eq0.z) /
(eq0.x * eq1.y - eq1.x * eq0.y)
Vec2D.new(ox, oy)
def am
2 * Matrix[
[points[0].x, points[0].y, 1],
[points[1].x, points[1].y, 1],
[points[2].x, points[2].y, 1]
].determinant
end

def bx
-Matrix[
[points[0].x * points[0].x + points[0].y * points[0].y, points[0].y, 1],
[points[1].x * points[1].x + points[1].y * points[1].y, points[1].y, 1],
[points[2].x * points[2].x + points[2].y * points[2].y, points[2].y, 1]
].determinant
end

def by
Matrix[
[points[0].x * points[0].x + points[0].y * points[0].y, points[0].x, 1],
[points[1].x * points[1].x + points[1].y * points[1].y, points[1].x, 1],
[points[2].x * points[2].x + points[2].y * points[2].y, points[2].x, 1]
].determinant
end
end
17 changes: 17 additions & 0 deletions lib/math_demo/matrix_math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Matrix Math ###

For detailed workings see [Circumcircle at Mathworld Wolfram.com][circumcircle]


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}}

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}}

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}}

xo = -bx / 2 * a

yo = -by / 2 * a


[circumcircle]:http://mathworld.wolfram.com/Circumcircle.html
11 changes: 0 additions & 11 deletions lib/math_demo/pbisector.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/math_demo/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
# A wrapper for version
module MathDemo
VERSION = '0.0.1'.freeze
VERSION = '0.1.0'.freeze
end
28 changes: 0 additions & 28 deletions test/pbisector_test.rb

This file was deleted.