views:

220

answers:

6

I'm trying to get an if statment that says if my Points Array at "i" (Initialized in a For Loop) is equal to the X and Y of a Circle (Which is set to smallcircle.X and smallcircle.Y). I know what I need to do in the if statment, but I can't get the if statment itself to work. What is the syntax for that?

Currently Have:

if (centerPoints[i] == smallcircle.X, smallcircle.Y)

It doesn't like that one bit.

+11  A: 

Probably this:

if (centerPoints[i].X == smallcircle.X && centerPoints[i].Y == smallcircle.Y)
RichieHindle
+2  A: 
if ((centerPoints[i].X == smallcircle.X) && (centerPoints[i].Y == smallcircle.Y))
Yuval A
+4  A: 

centerPoints[i] is an instance of Point, right?

In which case, this...

if (centerPoints[i] == new Point(smallcircle.X, smallcircle.Y))

... should do the trick

update

What type is your Circle? If it's yours then why doesn't it have a Centre property of type Point. This would make your life easier.

Martin Peck
Don't like this. A new object on every check?
Luca Martinetti
@Luca Point is actually a struct. Still not a reason to create a new one every time, but there's no overhead of GC.
BFree
Unnecessary object creation. Also, I find it less readable, but that's a matter of preference.
Yuval A
+2  A: 

You're looking for the logical AND operator, which in C# is &&. It's used to check if both conditions are true.

So that: if( pts[i] == smallcircle.X && pts[i] == smallcircle.Y ) { // Both are true so do this... }

If you want to check if EITHER condition is true, use the logial OR operator, ||: if( pts[i] == smallcircle.X || pts[i] == smallcircle.Y ) { // Either one or the other condition is true, so do this... }

LPalmer
+2  A: 

One more thing:

If this is the only thing your for loop is doing, you can write the whole loop like this:

foreach (Point point in centerPoints.Where(p => p.X == smallcircle.X && p.Y == smallcircle.Y) )
{
    //
}
Joel Coehoorn
A: 

Another option is to override the Equals() method in your Point struct, here's an example of one that I've used:

struct Point
{
    public int X;
    public int Y;

    public Point(int x, int y) { X = x; Y = y; }

    public override bool Equals(object obj)
    {
        return obj is Point && Equals((Point)obj);
    }

    public bool Equals(Point other)
    {
        return (this.X == other.X && this.Y == other.Y);
    }

    public override int GetHashCode()
    {
        return X ^ Y;
    }

    public override string ToString()
    {
        return String.Format("({0}, {1})", X, Y);
    }

    public static bool operator ==(Point lhs, Point rhs)
    {
        return (lhs.Equals(rhs));
    }

    public static bool operator !=(Point lhs, Point rhs)
    {
        return !(lhs.Equals(rhs));
    }

    public static double Distance(Point p1, Point p2) 
    {
        return Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
    }
}

So your code would become:

Point smallcircle = new Point(5, 5);
if (centerPoints[i] == smallcircle)
{
    // Points are the same...
}
John Rasch