| b1b2b3b4 (35) | |
|
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 ! | |
|
|
|
| Chervil (806) | |
| 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 <= . | |
|
|
|
| b1b2b3b4 (35) | |
|
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.. | |
|
|
|
| Chervil (806) | |
| Well, for example, if (x<0) it doesn't matter what is the value of y, the point is definitely outside the square.... | |
|
|
|
| Duoas (6732) | |
| 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. | |
|
|
|
| b1b2b3b4 (35) | |
| That is true. I see how simple this was when I drew it out. Thanks! | |
|
|
|
| Duoas (6732) | |
|
: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. | |
|
|
|
| b1b2b3b4 (35) | |||||
|
There is a greater problem now. I used two arrays for data storage.
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
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? | |||||
|
|
|||||
| Chervil (806) | ||||||||
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.
There are many ways, here's one example.
A single point:
Or an array of points:
| ||||||||
|
Last edited on
|
||||||||
| b1b2b3b4 (35) | |||
|
@Chervil If I have to input a lot of shapes, is
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? | |||
|
|
|||
| Chervil (806) | |||
|
Well, I don't know how this fits with your other requirements. Building on what has gone before,
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. | |||
|
|
|||
| b1b2b3b4 (35) | |||||||
It is currently like this
for the main program:
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
|
|||||||
| Chervil (806) | |
|
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. | |
|
|
|
| b1b2b3b4 (35) | |
|
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.. | |
|
|
|