# Distance of a circle?

**URL:** <https://forum.kirupa.com/t/distance-of-a-circle/260894>\
**Category:** flash\
**Created:** [May 20, 2008, 8:33pm UTC](https://forum.kirupa.com/t/distance-of-a-circle/260894 "2008-05-20T20:33:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yahoom](https://avatars.discourse-cdn.com/v4/letter/y/a587f6/32.png) [@yahoom](https://forum.kirupa.com/u/yahoom)\
**Post date:** [May 20, 2008, 8:33pm UTC](https://forum.kirupa.com/t/distance-of-a-circle/260894/1 "2008-05-20T20:33:02Z")

</div>

Hi everybody,

I’m building a touchscreen project that consists of flash and max/msp using Olaf Matthes flashserver as bi-directional control.

Soooo…I’m wondering how I create a function in flash that will provide me with the distance of a concentric circle, so that as it grows I can use trace() to give me a constant stream of it’s x & y coordinates?

I’ve also implemented a collision detection between two or more circles for the purpose of an overlap function. Trouble being that it’s not quite working properly. I want the overlap function for the purpose of morphing sounds between the intersecting parts of the circles.

I’d really appreciate any help anyone could offer with this. Sorry if it’s clear as mud.

Here’s the code I’m using:

/\*

```
flash frontend - simple flash gui for Max/MSP
(c) 2002 - 2003 Olaf Matthes &lt;olaf.matthes@gmx.de&gt;

http://www.nullmedium.de/dev/flashserver/

```

\*/

fscommand(“allowscale”, false);  
fscommand(“fullscreen”, false);

```
function maxsendvars (symbol, value) {
	fudi = symbol + " " + value + ";"
	max.send(fudi);
}

server = "localhost";
port = 31337;
_root.max = new XMLSocket();
max.connect(server, port);
max.onConnect = onMaxConnect;
max.onClose = onMaxClose;
max.onData = onMaxData;

function onMaxConnect (success) {
	if (success) {
		msg = "Connected to "+server+" on port "+port;
	} else {
		msg = "There has been an error connecting to "+server+" on port "+port;
	}
}

function onMaxClose () {
	msg = "Lost connection to "+server+" on port "+port;
}	
	/* receive data from flashserver */
function onMaxData(doc) {
	msg = "Received data from server: " + doc;
}

```

circles = new Array();

circle.\_visible = false;

var drawCFlag:Boolean = true;

listener = new Object(); /  
listener.onMouseDown = function(){  
\_root.\_quality = “MEDIUM”;  
instructions.\_visible = false;  
msDown = true;  
}  
listener.onMouseUp = function(){  
msDown = false;  
drawCFlag = true;  
}  
Mouse.addListener(listener);

var circleCount = 0;  
this.onEnterFrame = function(){ /  
if (msDown==true && drawCFlag == true){ /  
circleCount++;  
var newCircle:MovieClip = \_root.circle.duplicateMovieClip(“circle”+circleCount, circleCount);  
circles.push(newCircle);  
\_root[“circle”+circleCount].\_x = \_root.\_xmouse; \_root[“circle”+circleCount].\_y = \_root.\_ymouse;  
trace ("[x value= " + \_root.\_xmouse + “]”)  
trace ("[y value= " + \_root.\_ymouse + “]”)  
trace ("[circleCount= " + circleCount + “]”)  
trace ("[circles size = " + circles.length + “]”)  
\_root.max.send("; x\_value " + \_root.\_xmouse + “;”);  
\_root.max.send("; y\_value " + \_root.\_ymouse + “;”);  
drawCFlag = false;  
}  
andgo();  
}

function andgo(){

//Collision detection:  
for(var i:Number = 0;i \< circles.length;i++) {  
var src:MovieClip = circles\*;  
for(var j:Number = 0;j \< circles.length;j++) {

```
		// dont check with ourself
		if(i == j) continue;
		var dest:MovieClip = circles[j]; //Variable: dest [j]
		var touching:Number = checkOverlap(src,dest); 
		if(touching) //If they are touching [true] show results on flashes output
		{
			trace("Circles : " + src._x + " " + src._y + " : " + dest._x + " " + dest._y + " overlap");
		}
	}
}

```

}

function checkOverlap(c1:MovieClip, c2:MovieClip):Number  
{  
// Trigonometry: distance between centers  
var dx:Number = c2.\_x - c1.\_x;  
var dy:Number = c2.\_y - c1.\_y;  
var dist:Number = Math.sqrt( (dx\*dx) + (dy \* dy) );

```
if(dist &lt; c1._width + c2._width) //If distances is more than circle 1 + circle 2 width
{
	return Number(true)//Overlap is true
	}
	else //Otherwise
	{
	return Number(false); //Overlap is false
}

```

}

PS I’m still finding my feet with all this scripting malarky so don’t be too hasty to pick on me if I’ve done something wrong 🙂

---

<div class="post-metadata">

**Author:** ![Pier25](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pier25](https://forum.kirupa.com/u/Pier25)\
**Post date:** [May 21, 2008, 10:04am UTC](https://forum.kirupa.com/t/distance-of-a-circle/260894/2 "2008-05-21T10:04:04Z")

</div>

To calculate a distance between two points you need to use the pythagorean theorem

> **[Pythagorean theorem](https://en.wikipedia.org/wiki/Pythagorean_theorem)**
>
> In mathematics, the Pythagorean theorem, also known as Pythagoras' theorem, is a fundamental relation in Euclidean geometry among the three sides of a right triangle. It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the "Pythagorean equation": where c represents the length of the hypotenuse and a and b the le...

If the two objects are circles you just need to substract the two radius and you get the distance from border to border, not from center to center.

hope that helps 😉

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [May 21, 2008, 10:10am UTC](https://forum.kirupa.com/t/distance-of-a-circle/260894/3 "2008-05-21T10:10:19Z")

</div>

MATHS!!! omg, don’t you know your own code _cough_

// Trigonometry: distance between centers  
var dx:Number = c2.\_x - c1.\_x;  
var dy:Number = c2.\_y - c1.\_y;  
var dist:Number = Math.sqrt( (dx\*dx) + (dy \* dy) );

saying that if it is concentric… circle shaped, you can just use \_width to know how big it is, with the centre point being in the middle of the mc… surely
