My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Reply
 
LinkBack Thread Tools Display Modes
December 10th, 2013, 07:39 AM   #1
Newbie
 
Joined: Dec 2013

Posts: 2
Thanks: 0

Equation detecting overlapping square and circle.

So I'm new here, and am not sure how to ask questions properly, but here's my best shot.

I come from a website called Codecademy, learning programming with javascript (I now know more like python, java, and html/css), and moved to a website Khanacademy, to make some projects. One such project (that can be found here: Moveable Square) that I have been working on includes a square that can move around to find coins.

The trouble I'm having is in order for the square to "eat" the coin, I have to be able to detect when the square is over the coin (by the way the square is a square, and the coin is a circle if you haven't figured that out yet). I have tried complex expressions in an if statement, and I've also tried many nested if statements. So I decided to come here to ask some people who have been doing this for some time now.

So here is the information:

Here is the basic code to draw the square:
Code:
//Avatar means square
var avatarSize = 50;
var avatarCoordX = 175;
var avatarCoordY = 175;
//var avatarSpeed = 10;
//var avatarColor = color(120, 0, 255);
//var coinsEaten = 0;
To place the square you pass
Code:
(avatarCoordX, avatarCoordY, avatarSize, avatarSize)
as parameters to the rect() function.

The Coin has similar atributes:
Code:
var coinSize = 20;
var coinCoordX = 20;
var coinCoordY = 20;
//var coinColor = color(255, 245, 30);
ellipse(coinCoordX, coinCoordY, coinSize, coinSize);
On the canvas, if you click on the link to Khanacademy, the origin(0,0) is in the Top Left corner, and X+ is to the right, and Y+ is Down, kinda mirrored from a regular coordinate plane. So, given the Coordinates of the square avatar (avatarCoordX, avatarCoordY) and the coordinates of the circular coin (coinCoordX, coinCordY), how do you find when both will be overlapping.
Abomb1126 is offline  
 
December 10th, 2013, 02:09 PM   #2
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 16,046
Thanks: 938

Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms
Re: Equation detecting overlapping square and circle.

Do you want to test if the circle is entirely inside the square, has any overlap with the square, or something else?
CRGreathouse is offline  
December 11th, 2013, 07:36 AM   #3
Newbie
 
Joined: Dec 2013

Posts: 2
Thanks: 0

Re: Equation detecting overlapping square and circle.

Kinda both..... I want a function to return true if the square is touching the circle at all.... And that includes both completely on it, and partially touching it.
Abomb1126 is offline  
December 11th, 2013, 12:01 PM   #4
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 16,046
Thanks: 938

Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms
Re: Equation detecting overlapping square and circle.

Suppose you have the center of the circle (x, y) with radius r and the center of the square (X, Y) with side length S. You can compute the distance from the square's center to a corner as S/sqrt(2) = R. As a first test, if the distance from (x, y) to (X, Y) is more than r + R then the two don't overlap. As a second test, if the distance is less than r + S/2 then the two do overlap.

If the tests both fail -- the distance is between r + S/2 an r + R -- then you have more work to do. You can take each of the four sides of the square and test if they intersect with the circle by finding the distance from the line to the center of the circle. If this is more than the radius r of the circle, then go to the next side -- this one doesn't touch. Otherwise compute the points of intersection and see if they're within the square or outside it. (That is, the line you form from the side of a square extends beyond it in both directions, and the intersection might be on the line but not on the line segment that forms part of the square.) As long as at least one of the sides touches the circle you're good. You can do this just by solving the usual quadratic equation.
CRGreathouse is offline  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
circle, detecting, equation, overlapping, square



Search tags for this page
Click on a term to search for related topics.
Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Polar function from a (circle and square) shape triplekite Calculus 2 June 14th, 2013 02:00 PM
Detecting disease Keroro Algebra 2 June 7th, 2012 05:25 AM
Maximum Area of Circle and Square zerostalk Algebra 6 January 10th, 2012 08:06 PM
Any conformal map from square to unit circle? lumiere137 Complex Analysis 0 October 19th, 2010 05:00 AM
overlapping circles rockey191 Algebra 2 July 3rd, 2009 02:08 AM





Copyright © 2019 My Math Forum. All rights reserved.