For example I have 4 sets of Coordinates

``````(1,1)
(1,3)
(3,3)
(3,1)``````

It's the out line of a Square Shape

Is there a way I can find the list of sets of Coordinates on it's parameter?
The expected out put should be:

``````(1,2)
(2,1)
(2,3)
(3,2)``````

and is there a way I can find the list of sets of Coordinates within the Shape?
The expected out put should be:
(2,2)

Note that the set of coordinates I used isn't Array
but I declared them individually int x1 x2 x3 x4 y1 y2 y3 y4

I've come out with something like this:

``````if (s[x].getPX1() == s[x].getPX2())
{
if (s[x].getPY1() < s[x].getPY2())
{
int yy = s[x].getPY1()+1;
for (yy;yy < s[x].getPY2();yy++)
{
cout << "(" <<  s[x].getPX1() << ", " << yy << ")" <<endl;
}
}
else if (s[x].getPY1() > s[x].getPY2())
{
int yy = s[x].getPY2()+1;
for (yy;yy < s[x].getPY1();yy++)
{
cout << "(" <<  s[x].getPX1() << ", " << yy << ")" <<endl;
}
}
if (s[x].getPX2() < s[x].getPX3())
{
int xx = s[x].getPX2()+1;
for (xx; xx < s[x].getPX3(); xx++)
{
cout << "(" <<  xx << ", " << s[x].getPY2() << ")" <<endl;
}
}
else if (s[x].getPX2() > s[x].getPX3())
{
int xx = s[x].getPX3()+1;
for (xx; xx < s[x].getPX2(); xx++)
{
cout << "(" <<  xx << ", " << s[x].getPY2() << ")" <<endl;
}
}

if (s[x].getPY3() < s[x].getPY4())
{
int yy2 = s[x].getPY3()+1;
for (yy2; yy2 < s[x].getPY4(); yy2++)
{
cout << "(" << s[x].getPX3() << ", " << yy2 << ")" <<endl;
}
}
else if (s[x].getPY3() > s[x].getPY4())
{
int yy2 = s[x].getPY4()+1;
for (yy2; yy2 < s[x].getPY3(); yy2++)
{
cout << "(" << s[x].getPX3() << ", " << yy2 << ")" <<endl;
}
}
if (s[x].getPX4() < s[x].getPX1())
{
int xx2 = s[x].getPX4()+1;
for (xx2; xx2 < s[x].getPX1(); xx2++)
{
cout << "(" << xx2 << ", " << s[x].getPY4() << ")" <<endl;
}
}
else if (s[x].getPX4() > s[x].getPX1())
{
int xx2 = s[x].getPX1()+1;
for (xx2; xx2 < s[x].getPX4(); xx2++)
{
cout << "(" << xx2 << ", " << s[x].getPY4() << ")" <<endl;
}
}
}``````

It's kinda inefficient this way. Is there a better way to handle this?
Note that the code isn't even completed yet, the part just handle only if user input the "Vertical" coordinates first, I'll still have to come out with the "Horizontal" coordinates first scenario.
The coordinates will be input in an ordered sequence, but will not restrict where to begin.

Any help will be appreciated!

Edited by andylbh: post code

3
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by StuXYZ

>>Is there a way I can find the list of sets of Coordinates on it's parameter?
>>The expected out put should be:

I still don't know what exactly you mean by that. Can you explain a bit more.

Imagine the coordinates like this forms a Square:

``````(1,3)     (3,3)
.            .

.            .
(1,1)     (3,1)``````

This is the the data I have now from user input
It's just the 4 sets of coordinates to form a square shape.
When you connect the coordinates (draw on paper),
the lines will pass through (1,2), (2,1), (3,2), (2,3)
This is my first function/problem.
I also have to get (2,2) that is within the Square Shape.
This is my second function/problem.

Edited by andylbh: n/a

Seems pretty easy, all you have to do is use the points to calculate the 6 lines. So you can calculate a line from (1,1) to (1,3) then from (1,1) to (3,1) then (1,1) to (3,3) and so on. So to start

1) Create a function that calculates the slope given two coordinates
2) Create a function that takes a slope and two coordinates and prints all values between the two coordinates
3) Apply the above procedure for the six lines you need.

If you are just after the centre and the shape is convex then, what about just taking the average if you do

``````Point C(0,0)

// sum over points:
for(int i=0;i<Number_of_Points;i++)
C+= shapePoint[i];

// divide by the number added up:
C/=Nubmer_of_Points;``````

(Point is just a simple x,y class. You can equally do it with a `struct Point { double x,y; };` , and just add to x and y. )

That gives you a centre of mass, that will be in the shape if the shape is convex.

[note you might have to deal with floating point coordinates after the division. Then you will have to either accept them or if you need integer, then test the four possible roundings (e.g 3.3,4.3) test (3,4), (4,4), (4,5) and (3,5). Even then that might fail. [But in that case there will be no solution]].

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.