Rectangle or Square Calculator

Hey guys, I am supposed to write a program with a set of points, x1,y1 x2,y2 x3,y3 and x4,y4. This program is supposed to take the points into effect, and tell wether or not it is a rectangle or square. It keeps telling me no matter what set of point i put as arguments, that it is a rectangle. What am i doing wrong?

Thanks for any help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include "Quadralateral.h"
#include <iostream>

Quadralateral::Quadralateral(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
  X1 = x1;	// memberwise assignment
  Y1 = y1;
  X2 = x2;
  Y2 = y2;
  X3 = x3;
  Y3 = y3;
  X4 = x4;
  Y4 = y4;
}

bool Quadralateral::isRectangle() // this is correct...i think
{
	
	double diag1, diag2;
	diag1 = sqrt(pow(double(X3 - X1),2) + pow(double(Y3 - Y1),2));
	diag2 = sqrt(pow(double(X4 - X2),2) + pow(double(Y4 - Y2),2));


	if(abs(diag1 - diag2) < .000001)
		return true;
	else
		return false;

}



bool Quadralateral::isSquare()
{
			double diag1, diag2, side1, side2, side3, side4;
    diag1 = sqrt(pow(double(X3 - X1),2) + pow(double(Y3 - Y1),2));
    diag2 = sqrt(pow(double(X4 - X2),2) + pow(double(Y4 - Y2),2));

	side1 = (X1+Y1);
	side2 = (X2+Y2);
	side3 = (X3 + Y3);
	side4 = (X4 + Y4);


    if ((abs(diag1 - diag2) < .000001) && (side1 == side3))  // is this the right way to do this?
        return true;
    else 
        return false;

}

Quadralateral::~Quadralateral(void)
{

}


HEADER FILE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
class Quadralateral
{
public:
	Quadralateral(int=13,int=10,int=13,int=20,int=23,int=20,int=23,int=10);
	bool isRectangle();
	bool isSquare();



	~Quadralateral(void);
private:
	int X1,Y1,X2,Y2,X3,Y3,X4,Y4;
};

There's another thread on the same problem:
http://www.cplusplus.com/forum/beginner/187209/
Your algorithm is wrong. Check it out on paper - given corners (0,0), (3,0), (3,3), (0,3) how does your algorithm check that this is a square?

Then also check for (0,0), (2,2), (0,4), (-2,2)
Topic archived. No new replies allowed.