### 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.

 ``12`` ``````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

 ``12`` ``````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.
 ``1234`` `````` struct point { double x; double y; };``````

A single point:
 ``1234`` `````` point test = {5,12}; cout << "x: " << test.x << endl; cout << "y: " << test.y << endl;``````

Or an array of points:
 ``1234567`` `````` 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
 ``1234567`` `````` 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,

 ``123456789101112131415161718192021222324`` ``````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
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````// shape.h #include #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 ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````// square.h #ifndef SQUARE_H #define SQUARE_H 1 #include #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. "<>x; cout<<"Please enter y-ordinate of pt. "<>y; } } #endif ``````

for the main program:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````#include #include #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"<>menuChoice; menuChoice = toupper(menuChoice); switch(menuChoice) { case '1': cout<>shape; break; case '2': cout<<"Print"<

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.