0

I would like to find each integer coordinate point between 2 set of coordinate points.

For example, I need the coordinates between (2,15) (6,15). It should give me (3,15) (4,15) (5,15) . I cannot find any math formula or c++ code that does this.

I want all the coordinates on the line connecting the two points where both X and Y happen to be integers

(6,15)&(6,17) = (6,16)

The coordinates form a shape such as Rectangle or cross and the basic idea is to get the coordinates between each set of coordinates.

Rectabgle Shape
Point [1] : (2, 17)
Point [2] : (2, 15)
Point [3] : (6, 15)
Point [4] : (6, 17)

Points on perimeter : (2, 16), (3, 15), (4, 15), (5, 15), (6, 16), (5, 17), (4, 17), (3, 17)

Hope this explains better on what I want to achieve.

egrunin
  • 24,650
  • 8
  • 50
  • 93
rasul1719435
  • 115
  • 4
  • 10
  • Coordinates are not float point? – Denis Ermolin Oct 24 '12 at 18:44
  • 2
    Define coordinates between 2 coordinates. – max Oct 24 '12 at 18:44
  • 3
    What about points that don't lie on the same x or y axis? For example, what coordinates are there between (0,0) and (5,7)? – Kevin Oct 24 '12 at 18:49
  • @Kevin No i dont need it. only from 2 specific points – rasul1719435 Oct 24 '12 at 18:50
  • 2
    @rasul1719435 - what answer would you expect from (2,15) to (6,16)? – Robᵩ Oct 24 '12 at 18:56
  • 2
    Your question is not clear. For instance if you take the straight line segment defined by (0,0) and (3,3) the points (1,1) and (2,2) are in this segment. If you take (0,0) and (2,1) the point (1,0.5) is also in the straight line linking the extremities, but no point with integer only coordinates exist. So, can you consider floating point or not? If only integers are allowed, do you need only exact solutions or approximate solutions are also acceptable? – rpsml Oct 24 '12 at 19:00
  • As a further follow-up; if you only need 12 sets of coordinates, but there are more than 12 points between the two, how do you want to select the twelve you keep? – Nathaniel Ford Oct 24 '12 at 19:01
  • @Robᵩ The coordinates u give dont create a straight line. – rasul1719435 Oct 24 '12 at 19:05
  • He either wants a pixel-plotting algorithm (which 'fakes' a straight line) or the coordinate pairs that happen to be integers. I'm not sure which. – egrunin Oct 24 '12 at 19:08
  • @rpsml Yes I can consider floating points. The reason i say integers was the points are axis aligned thats y. So i assume it to be integers. Plus I would prefer similar c++ solutions. not really exact answers. – rasul1719435 Oct 24 '12 at 19:09
  • @Kevin The coordinates are (4,6) (3,5) (2,4) (1,3) (1,2) (1,1) (0,0) – rasul1719435 Oct 24 '12 at 19:19
  • In that case, the answer by @Max, below, is pretty much what you want. You just need to calculate `m` and `c` (standard analytical geometry, you can wiki straight line for the formulas) and define how many points you want. – rpsml Oct 24 '12 at 20:02
  • @rpsml can u explain further, or may be code snippets or even pusedo code – rasul1719435 Oct 24 '12 at 20:19

2 Answers2

7

Trying to find all the coordinates on the line connecting the two points where both X and Y happen to be integers:

First, as another poster pointed out, you need to put this in the form y = mx + b:

int StartX = 0;
int StartY = 10;
int EndX = 100;
int EndY = 110;

// m = rise / run
float m = 0.0;

if (EndX == StartX || EndY == StartY)
{
    // add code here to handle the trivial cases
    return;
}

int run = EndX - StartX;
int rise = EndY - StartY;
m = ((float) rise) / ((float) run);

// solve for b
// (start with y = mx + b, subtract mx from both sides)
float b = StartY - (m * StartX);

Now iterate:

// note: assumes EndX > StartX
for (int x = StartX; x <= EndX; ++x)
{
    // solve for y
    float y = (m * x) + b;

    // round to nearest int
    int rounded = (y > 0.0) ? floor(y + 0.5) : ceil(y - 0.5);

    // convert int result back to float, compare
    if ((float) rounded == y)
        printf("(%d, %d)\n", x, rounded);
}

This code is not tested, and it fails if EndX < StartX, but it should get you started. If someone has a better comparison method, let me know and I'll include it.

Edited to add

The question has been closed, probably because it was not stated clearly, but I've added a line to show where you need to handle StartX == EndX and StartY == EndY.

egrunin
  • 24,650
  • 8
  • 50
  • 93
5

If you need to plot the points efficiently, use Bresenham's algorithm or the Mid-point line algorithm.

If you just need the points, then:

Express the line between the points as y = mx + c

Initialize x to x-coordinate of one of the vertices. Iterate from first x-coordinate to the other, increment x based on the number of points you want, the y-coordinate for this x will be m*x+c

Untested, assuming x1 < x2

struct Point {
     double x;
     double y;
};

Point* getPointsBetween(int x1, int y1, int x2, int y2, int numOfPoints) {
    double m = (y2-y1)/(x2-x1);
    double c = y1 - m*x1;

    Point *points = new Point[num];
    double increment = (x2-x1)/numOfPoints);

    for(double i=x1+increment ; i<x2 ; i+=increment) {
        points[i].x = i;
        points[i].y = m*i + c;
    }

    return points;
}
max
  • 4,248
  • 2
  • 25
  • 38