Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Programming exercise

Name: Anonymous 2009-02-08 6:52

I recently found a website with multiple exercises to improve your logical thinking and programming at the same time, and I'm almost done with this one exercise, I'm just having problems with ONE small part. Now, it's not too important because I can simply move on to the next one, but it's bugging me and I'd like to find a way to solve it.

Quick description of the exercise:
You have a .txt with a bunch of random coordinates (x,y). Those coordinates are points for squares. Using the coordinates, you have to find out all of the possible squares and then print them out.

I have every algorithm done and programmed except the verification. Basically, what I'm trying to do is that once I've got my 4 points, I want a way to find out whether it's a square or not. I have a way that ``somewhat'' works, but it feels like a hack and I feel there should be a much simpler way.

bool isSquare(const Point& pt1, const Point& pt2, const Point& pt3, const Point& pt4){
}


What I'm doing right now is I calculate the distance (
float calculateDistance(const Point& pt1, const Point& pt2)
{
    float distance = 0.0;
    float deltaX = ((pt2.getX() - pt1.getX() * (pt2.getX() - pt1.getX())));
    float deltaY = ((pt2.getY() - pt1.getY() * (pt2.getY() - pt1.getY())));
    distance = sqrt(deltaX - deltaY);

    return distance;
}
) between pt1 and pt2, then check all the other sides. If I get two other sides of the same length => it's a side, if I get only one => it's a diagonal. I repeat that for combination of pt. At the end, if I get 4 sides and 2 diagonals, it's a square and then I add it to my end result vector. If not, I trash it.

Not urgent or anything, I'd just like to know whether anyone here has a better idea.

Name: Anonymous 2009-02-08 16:41

Hint: c2 = a2 + b2

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List