# Geometric Intersections (#233)

**URL:** <https://www.ruby-forum.com/t/geometric-intersections-233/189344>\
**Category:** Ruby\
**Created:** [May 29, 2010, 1:30am UTC](https://www.ruby-forum.com/t/geometric-intersections-233/189344 "2010-05-29T01:30:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [May 29, 2010, 1:30am UTC](https://www.ruby-forum.com/t/geometric-intersections-233/189344/1 "2010-05-29T01:30:31Z")

</div>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q.:

1. Please do not post any solutions or spoiler discussion for this  
quiz until 48 hours have elapsed from the time this message was  
sent.

2. Support Ruby Q. by submitting ideas and responses  
as often as you can.

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem  
helps everyone on Ruby T. follow the discussion. Please reply to  
the original quiz message, if you can.

* * *

RSS Feed: [http://rubyquiz.strd6.com/quizzes.rss](http://rubyquiz.strd6.com/quizzes.rss)

Suggestions?: [http://rubyquiz.strd6.com/suggestions](http://rubyquiz.strd6.com/suggestions)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Geometric Intersections (#233)

HIâŸ‚RB

This week’s quiz is to detect the intersections of various geometric  
elements: circles, lines, and polygons.

A circle is defined by a point and a radius.

point = [6, 3]  
radius = 5  
a = Circle.new(point, radius)

A line can be defined by two points, or a point and a slope.

p1 = [0, 0]  
p2 = [4, 2]  
line = Line.new(p1, p2)  
slope = 0.5  
line = Line.new(p1, slope)

A polygon can be defined as a collection of points.

b1 = [2, 9]  
b2 = [1, 3]  
b3 = [7, 5]

b = Polygon.new(b1, b2, b3)

def intersect(a, b)

# Should return true or false indicating whether or not

# a and b intersect. a and b can be any circle, line or

# polygon.

end

You may also find it more natural to define a.intersect(b) or  
b.intersect(a). The relation should be symmetric and reflexive, though  
not transitive.

Which methods you choose to add to the geometric elements is up to  
you, the only hard requirement is that the intersect method(s) return  
the correct truth values.

Have fun!

Extra Credit: handle the “edge” cases of the polygon comprised of one  
point (a single point) or two points (a line segment).

Extra Extra Credit: Provide a graphical and colorful output indicating  
intersections.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [June 1, 2010, 1:44am UTC](https://www.ruby-forum.com/t/geometric-intersections-233/189344/2 "2010-06-01T01:44:57Z")

</div>

Hei everybody,

here [GitHub - bolthar/intersect: Solution to ruby quiz #233](http://github.com/bolthar/intersect/) is my solution. You will  
need  
freightrain ([GitHub - bolthar/freightrain: Ruby desktop development made easy](http://github.com/bolthar/freightrain/) to run it.  
intersect.rb is a script that can be run with an argument, which is the  
path to the filename containing the shapes definition. The file  
‘testfile’ in the lib directory is an example of the syntax.  
intersect\_gui.rb launches a GUI application that you can play with.

–  
Andrea D.

> **[GitHub - bolthar/freightrain: Ruby desktop development made easy](https://github.com/bolthar/freightrain)**
>
> Ruby desktop development made easy. Contribute to bolthar/freightrain development by creating an account on GitHub.

> **[using (IMHO)](https://usingimho.wordpress.com/)**
>
> Andrea Dallera 's blog

On Sat, 2010-05-29 at 08:29 +0900, Daniel M. wrote:

> Suggestions?: [http://rubyquiz.strd6.com/suggestions](http://rubyquiz.strd6.com/suggestions)  
> A circle is defined by a point and a radius.  
> slope = 0.5  
> def intersect(a, b)  
> you, the only hard requirement is that the intersect method(s) return  
> the correct truth values.
> 
> Have fun!
> 
> Extra Credit: handle the “edge” cases of the polygon comprised of one  
> point (a single point) or two points (a line segment).
> 
> Extra Extra Credit: Provide a graphical and colorful output indicating  
> intersections.
