How to determine points on/in a square?

I have a square, with x y coordinates entered.

(0,0) (0,2) (2,2) (2,0)
This coordinates will make up a square.

The points on the square would be (0,1) (1,2) (2,1) (1,0)
The point in the square would be (1,1)

Is there any way I can find out the points using any methods?

Like it will return true when the point is on/in the square.

Help please !
Did you try sketching a quick diagram of this. You should be able to eliminate values which are outside your requirements using the comparison operators ==, >, >=, < and <= .
Logically, I don't quite understand where should I start, how do I eliminate the unwanted values. It has been puzzling me. Been searching through the internet.

Like how is it use that using those operators will found out the values that I need to find out..
Well, for example, if (x<0) it doesn't matter what is the value of y, the point is definitely outside the square....
Honestly, if you just get out a piece of paper and draw yourself a cartesian numberline and a square, then play with figuring out how to tell if a point is in or out of the square, then you will get it fairly quickly and have a very good understanding of how to make it work.
That is true. I see how simple this was when I drew it out. Thanks!
:O)

Believe it or not, that is usually one of the hardest things to teach people to do, because it seems so stupidly simple.

I could stuff cabinets with all the little pictures and equations I've drawn on random papers over the years.
There is a greater problem now.

I used two arrays for data storage.

1
2
Test array1[50]
Test array2[50][12]


This is fine, when storing names/anything else in array1[50].
I store the coordinates in array2 because I have no idea how to store 4 (x,y) points into 1 variable and extract them when trying to match them.

So whenever I try the virtual and dynamic way which is.
Adding a virtual and also changing the arrays to

1
2
Test *array1[50]
Test *array2[50][12]


For the first array1[50] with the new *. It is fine. Nothing is wrong.
But when it comes to the second new array, it hangs whenever I put this array in. Is there any way to go about this?
There's not enough information to see what's wrong with your code, it seems to be the use of new, but you'd need to show that code.

I store the coordinates in array2 because I have no idea how to store 4 (x,y) points into 1 variable and extract them when trying to match them.

There are many ways, here's one example.
1
2
3
4
    struct point {
        double x;
        double y;
    };

A single point:
1
2
3
4
    point test = {5,12};

    cout << "x: " << test.x << endl;
    cout << "y: " << test.y << endl;


Or an array of points:
1
2
3
4
5
6
7
    point square[4];
    square[0].x = 0.0;
    square[0].y = 0.0;

    square[1].x = 0.0;
    square[1].y = 2.0;
    // etc. 
Last edited on
@Chervil

If I have to input a lot of shapes,

is
1
2
3
4
5
6
7
    point square[4];
    square[0].x = 0.0;
    square[0].y = 0.0;

    square[1].x = 0.0;
    square[1].y = 2.0;
    // etc.  

a good way to store them? If so, how do I know what to extract when I am looking for square[0] ? Is there a better way to store all the four points of the square? i.e x and y.

I am also dealing with inheritance and polymorphism.

So in what way is it best, for me to store all the four points so that it is easier for me to extract information out when I need the first square, or the second triangle, or the third rectangle?
Well, I don't know how this fits with your other requirements.
Building on what has gone before,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct point {
        double x;
        double y;
};

struct triangle {
    point a;
    point b;
    point c;
};

struct square {
    point points[4];
};

    triangle t;
    t.a.x = 0;
    t.a.y = 1;
    // etc

    square s;
    s.points[0].x = 2;
    s.points[0].y = 2;
    // etc. 

These are just ideas to play with. I did triangle with individual corners, while the square has an array of 4 points.

I can't tell you whether they are suitable for your needs, in fact this may be a completely wrong direction.

You could extend this so the square, triangle etc. are proper classes with constructors etc. It really does depend on what are the full requirements that you have.

You could perhaps have a generic shape containing an array of points. Then depending on what type of shape it is, you will know how many points are in use.



It is currently like this
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
// shape.h
#include <string>
#ifndef SHAPE_H
#define SHAPE_H 1
using namespace std;

class Shape
{

	protected:
		int x, y;
		string name;
	public:
	// a simple inline constructor
	Shape(int new_x, int new_y, string new_name): x(new_x), y(new_y), name(new_name)
	{
		return;
	};
	
	virtual ~Shape()
	{
		return;
	};
	// inline getter/setter functions
	string getName() { return name; };
	void setName(string new_name)
	{
		name = new_name;
	}
	
	int getX() { return x; };
	void setX(int set_x)
	{
		x = set_x;
	}
	
	int getY() { return y; };
	void setY(int set_y)
	{
		y = set_y;
	}

	virtual void erase() = 0; // abstract method for erasing the object
	virtual void display() = 0; // abstract method for displaying the object
};


#endif 



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
// square.h
#ifndef SQUARE_H
#define SQUARE_H 1
#include <string>
#include "Shape.h"
using namespace std;

class Square : public Shape
{
    protected:
    int size;
    public:
    // a c'tor that calls the parent class's c'tor
    Square(int new_x, int new_y, string new_name, int new_size): Shape(new_x, new_y, new_name), size(new_size)
    {
    	return;
    };
	
	void setXY();
	
    virtual void erase();
    virtual void display();
};

void Square::setXY()
{
	arrayOfShapes[count] = new Square(4);
	for(int i=0; i<4; i++)
	{
		cout<<"Please enter x-ordinate of pt. "<<i+1;<<" : ";
		cin>>x;
		cout<<"Please enter y-ordinate of pt. "<<i+1;<<" : ";
		cin>>y;

	}
}

#endif 



for the main program:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include "Shape.h"
#include "Square.h"
using namespace std;

class Test
{
	public:
		void mainMenu();
		char menuChoice;
		void stringToUpper(string &s);
};

void stringToUpper(string &s)
{
   for(unsigned int l = 0; l < s.length(); l++)
  {
    s[l] = toupper(s[l]);
  }
}

void Test::mainMenu()
{
	cout<<"test"<<endl<<endl;
	cout<<"1)	input x y coords"<<endl;
	cout<<"2)	2"<<endl;
	cout<<"3)	3"<<endl;
	cout<<"4)	4"<<endl;
	cout<<"Q)	q to quit<<endl<<endl;
}

int main()
{
	char menuChoice;
	
	bool quit=false;
	Test test;
	int count=0;
	
	string shape, special;
	Shape *arrayOfShapes[10];
	
	while ( !quit )
	{
		test.mainMenu();
		cout<<"Please enter your choice : ";
		cin>>menuChoice;
		menuChoice = toupper(menuChoice);
		
		switch(menuChoice)
		{
				case '1':
					cout<<endl<<"input x,y coords"<<endl;
					cout<<"Please enter name of shape : "<<endl;
					cin>>shape;
					break;
				case '2':
					cout<<"Print"<<endl<<endl;
					break;	   	   	 
				case '3':
					cout<<"You choosen 3"<<endl<<endl;
					break;	 	 	 	 	 
				case '4':
					cout<<"You choosen 4"<<endl<<endl;
					break;
				case 'Q':
					cout<<"You have chosen to quit!"<<endl<<endl;
					quit=true;
					exit(0);	 	 	 	 
				default:
					cout<<"Invalid entry!"<<endl<<endl;
					break;
		}
	}
}

 


For the main program, I will also add in codes for the user to enter coords X and Y. Now the problem for me is, how do I input data for x,y and store them. I might play around with the ideas you gave me.

Do you get a clearer point of view here? Am I able to call the one at Square setXY, when I know that the input of the shape is 'Square' at the main program, then storing the coords?
Last edited on
The code is interesting, and I will look through it in more detail when I have time.

However, this tells me your proposed solution. It doesn't tell me what are your requirements, which would be even more useful.
Requirements is to be able to store x,y. Inheritance and polymorphism between shape and square and rectangle.

I want to know how can I store my x,y and be able to extract the information out easily.. I am stuck at this.

Another requirement is also determinating the points on the shape..
finding out the area..
Topic archived. No new replies allowed.