Reputation: 335
I am having trouble trying to write a method (in c#) that returns all of the integer lattice pairs within a circle of a given radius at a specific offset. I found this article https://en.wikipedia.org/wiki/Gauss_circle_problem but unfortunately it seems more interested in calculating the number of lattice pairs rather than identifying each individual lattice pair.
I am also having issues understanding some of the math terminology/symbols as my math is sadly somewhat lacking, so code examples or detailed explanations would be super helpful if possible.
my plan so far is to just check each integer value combination from the radius to negative radius and then simply check the distance to the origin, before applying the offset to the vectors that are within range.
Am i on the right track with this, or is there a more optimized way to achieve this?
example stub:
public class CircleTest ()
{
public List<Vector2>GetContainedIntegerVectors(float radius, Vector2 centerPosition)
{
//math goes here
}
}
Simple Vector2 class
public class Vector2 ()
{
public float x {get; set;}
public float y {get; set;}
}
Thanks!
Upvotes: 1
Views: 292
Reputation: 827
For my understanding you are on the right track, but there are some optimizations possible:
System.Windows.Vector
class from C# instead of your ownx>0
and y>=0
and mirror the rest (plus include centerpoint).here a possible implementation:
List<Vector> tmpList = new List<Vector();
List<Vector> list = new List<Vector();
double rSquared=r*r; // using sqared reduces execution time (no square root needed)
for(int x=1; x<=r; x++)
for(int y=0; y<=r; y++) {
Vector v = new Vector(x,y);
if(v.LengthSquared<=rSquared)
tmpList.Add(v);
else
break;
}
list.Add(centerVector);
foreach(Vector v in tmpList) {
Vector vMirr = new Vector(v.X, -1*v.Y);
list.Add(Vector.Add(centerVector, v));
list.Add(Vector.Add(centerVector, v.Negate()));
list.Add(Vector.Add(centerVector, vMirr));
list.Add(Vector.Add(centerVector, vMirr.Negate));
}
Upvotes: 2
Reputation: 335
public List<Vector2>GetContainedVectors(int radius, Vector2 offset)
{
List<Vector2> returnValues = new List<Vector2>();
for (int x = radius; x > -radius; x--)
{
for (int y = radius; y > -radius; y--)
{
if(Vector2.Distance(new Vector2(x,y), Vector2.zero) <= radius)
{
returnValues.Add(new Vector2(x + offset.x, y + offset.y));
}
}
}
return returnValues;
}
Upvotes: -1