Skip to content

Instantly share code, notes, and snippets.

@mrryanjohnston
Created August 17, 2020 12:55
Show Gist options
  • Save mrryanjohnston/919bbbcca5c1674d5243337d2cfd1e5d to your computer and use it in GitHub Desktop.
Save mrryanjohnston/919bbbcca5c1674d5243337d2cfd1e5d to your computer and use it in GitHub Desktop.
require 'durable'
Durable.ruleset :circle do
when_all(
c.point = (+m.x) & (+m.y),
c.circle = m.radius_squared == ( point.x * point.x ) + ( point.y * point.y )
) do
puts "point (#{point.x}, #{point.y}) " \
"is on the edge of circle #{circle.name}"
end
when_all(
c.point = (+m.x) & (+m.y),
c.circle = m.radius_squared < ( point.x * point.x ) + ( point.y * point.y )
) do
puts "point (#{point.x}, #{point.y}) " \
"is outside of circle #{circle.name}"
end
when_all(
c.point = (+m.x) & (+m.y),
c.circle = m.radius_squared > ( point.x * point.x ) + ( point.y * point.y )
) do
puts "point (#{point.x}, #{point.y}) " \
"is inside of circle #{circle.name}"
end
end
Durable.assert :circle, {name: :a, radius_squared: 25, x: 0, y: 0}
Durable.assert :circle, {x: 0, y: 0}
Durable.assert :circle, {x: 1, y: 1}
Durable.assert :circle, {x: -1, y: -1}
Durable.assert :circle, {x: 5, y: -5}
Durable.assert :circle, {x: 5, y: 0}
Durable.assert :circle, {x: 0, y: 5}
Durable.assert :circle, {x: 2, y: 2}
Durable.assert :circle, {x: 3, y: 3}
$ ruby index.rb
point (0, 0) is inside of circle a
point (1, 1) is inside of circle a
point (-1, -1) is inside of circle a
point (5, -5) is outside of circle a
point (5, 0) is on the edge of circle a
point (0, 5) is on the edge of circle a
point (2, 2) is inside of circle a
point (3, 3) is inside of circle a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment