# Re: Overlapping circles - modelling living populations

**URL:** <https://www.ruby-forum.com/t/re-overlapping-circles-modelling-living-populations/68135>\
**Category:** Ruby\
**Created:** [September 9, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/re-overlapping-circles-modelling-living-populations/68135 "2006-09-09T17:46:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 9, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/re-overlapping-circles-modelling-living-populations/68135/1 "2006-09-09T17:46:32Z")

</div>

Here’s a slightly better version that uses the overlapping status of a  
circle to change its color. (It doesn’t do anything with pairs of  
circles, though that’d also be easy…you just have to know how you’d  
want to visualize it.)

# [http://downloads.sourceforge.jp/ruby-svg/2288/ruby-svg-1.0.3.tar.gz](http://downloads.sourceforge.jp/ruby-svg/2288/ruby-svg-1.0.3.tar.gz)

require ‘svg/svg’

class Array  
def each\_unique\_pair  
self.each\_with\_index{ |a,i|  
self[(i+1)…-1].each{ |b| yield a,b }  
}  
end  
end

class SVG::Circle

# test (and save) result

def overlaps?( other\_circle )  
center\_distance = Math.sqrt(  
( cx - other\_circle.cx ) \*\* 2 +  
( cy - other\_circle.cy ) \*\* 2  
)

```
intersects = center_distance < ( r + other_circle.r )

# Save the result on both circles
other_circle.overlaps[self] = intersects
self.overlaps[other_circle] = intersects

```

end

def overlaps  
# Make sure we have a hash to save a set of overlappers  
@overlapping\_circles ||= {}  
end

# An array of all circles this one overlaps

def overlapping\_list  
# Make sure we have a hash to save a set of overlappers  
@overlapping\_circles ||= {}  
@overlapping\_circles.select{ |circle,status|  
status == true  
}.map{ |circle,status|  
circle  
}  
end  
end

# Create/manage your populations

populations = []  
20.times{

# Randomly add populations

x = rand 800 + 100  
y = rand 800 + 100  
radius = rand 100 + 10  
populations \<\< SVG::Circle.new( x, y, radius )  
}

# Make sure the overlapping list is valid

populations.each\_unique\_pair{ |a,b|  
a.overlaps?( b )  
}

# Make the picture

svg = SVG.new(‘100%’, ‘100%’, ‘0 0 1000 1000’)

# Your grid here

0.step( 1000, 20 ){ |i|  
svg \<\< SVG::Line.new( i, 0, i, 1000 )  
svg \<\< SVG::Line.new( 0, i, 1000, i )  
}

# Make all circles have a common style

svg \<\< g = SVG::Group.new{  
self.style = SVG::Style.new(  
:fill =\> ‘#ffe’, :stroke =\> ‘blue’, :stroke\_width =\> 1.5  
)  
}

# Need opacity per element, not on the group

low\_opacity = SVG::Style.new( :opacity =\> 0.6 )

# Override style for circles that overlap

olap\_style = SVG::Style.new( :fill=\>‘#ff9’, :stroke=\>‘red’,  
:opacity=\>0.4 )

populations.each{ |circle|  
if circle.overlapping\_list.empty?  
circle.style = low\_opacity  
else  
circle.style = olap\_style  
end  
g \<\< circle  
}

File.open( ‘populations.svg’, ‘w’ ){ |file|  
file \<\< svg.to\_s  
}

# view in your favorite SVG viewer, like FF 1.5

# for an example, see [http://phrogz.net/svg/populations.svg](http://phrogz.net/svg/populations.svg)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 9, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/re-overlapping-circles-modelling-living-populations/68135/2 "2006-09-09T19:42:45Z")

</div>

Gavin,

On Sun, 2006-09-10 at 00:44 +0900, Gavin K. wrote:

> end  
> intersects = center\_distance \< ( r + other\_circle.r )
> 
> end
> 
> svg \<\< SVG::Line.new( i, 0, i, 1000 )
> 
> # Need opacity per element, not on the group
> 
> end  
> g \<\< circle  
> }
> 
> File.open( ‘populations.svg’, ‘w’ ){ |file|  
> file \<\< svg.to\_s  
> }
> 
> # view in your favorite SVG viewer, like FF 1.5
> 
> # for an example, see [http://phrogz.net/svg/populations.svg](http://phrogz.net/svg/populations.svg)

Many thanks for that! I will have to go over this in detail to  
understand what is happening but it looks nice! A couple of things:

1. There seems to be some doubt about the survival of SVG . .

2. Opera supports SVG natively! (which seems to go against the first  
point)

Regards,

## Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Mobile: +61:(0)411-185-652  
Fax: +61:(0)2-8221-9599  
E-mail: removed\_email\_address@domain.invalid
