Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the dimensions of a matrix and a given cell with radius.

So let's take a look at this example:
https://i.imgur.com/51hTJpv.png[Matrix]
The matrix has 5 rows and 6 columns. We are given the cell (2, 3) with radius 2. It has an impact, and it destroys all of the items in a certain radius (the impact cell is shaded black and the other cells within the radius are shaded grey). I found that I could use the Pythagorean Theorem to check whether a cell is inside the radius:
if (Math.Pow(targetRow - row, 2) + Math.Pow(targetColumn - col, 2) <= radius * radius)
{
    matrix[row, col] = 1; 
}

I don't understand why it works, and I would be very grateful if you could explain it to me. I tried to debug, but I still don't get it.

What I have tried:

if (Math.Pow(targetRow - row, 2) + Math.Pow(targetColumn - col, 2) <= radius * radius)
{
    matrix[row, col] = 1; 
}
Posted
Updated 22-May-19 8:07am

1 solution

This is basic maths.
targetRow - row is the horizontal distance from the target to the centre of your circle with radius radius, and targetColumn - col is the vertical distance. The radius by definition never shorter than the longest of the absolute values of those two, so it is always the hypotenuse. Since the square of any number is always zero or positive, your squares of the other two sides are always positive, and Pythagoras tells us that root(x2 + y2) is the distance from the centre to your point. If that is smaller than the radius, your point is inside the circle.
 
Share this answer
 
v2
Comments
Member 14422952 22-May-19 14:15pm    
Thank you for your response! I really appreciate that. But where there are circles? This is what confuses me.
OriginalGriff 22-May-19 14:20pm    
You have one circle, with radius radius, yes? And that the center where the "bomb" goes off, yes? Draw that, on squared background. Now add your point (x, y) to the diagram, and draw a circle with the same centre as your original one, and which intersects (x, y). That gives you the other radius, which if it's smaller than the original radius means (x, y) is inside your "blast radius".

Draw it on paper, and you'll see what I mean!
Member 14422952 22-May-19 15:09pm    
But can't I calculate the distance between the points using this formula and compare it with the given radius? https://www.mathplanet.com/education/algebra-2/conic-sections/distance-between-two-points-and-the-midpoint

OriginalGriff 23-May-19 2:13am    
That is what you are doing!
[no name] 22-May-19 14:46pm    
Looks like <sup>xyz</sup> conflicts inside a <code> tag

axyz
vs.
a<sup>xyz</sup>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900