0

So i have 4 points, p1,p2,p3 and p4. These move constantly to the left and move back to the far right (window width + 100) whenever they reach too far left (x-100). Their Y is random.

I also have lines drawn between each point, this makes a fluent "jagged" line moving across the screen (kind of like the CPU Usage chart in windows task manager).

    if (p1x < p2x) {
        g.drawLine(p1x, p1y, p2x, p2y);
    }
    if (p2x < p3x) {
        g.drawLine(p2x, p2y, p3x, p3y);
    }
    if (p3x < p4x) {
        g.drawLine(p3x, p3y, p4x, p4y);
    }
    if (p4x < p1x) {
        g.drawLine(p4x, p4y, p1x, p1y);
    }

I want a dot to have a constant X in the window, but moving with the line in the Y axis, how do I do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    I really dont understand what you are asking for. Please provide some more information or illustrations. – atomman Feb 02 '13 at 12:31

1 Answers1

2

I think you may be looking for linear interpolation:

// assuming p0x and p0y are the coordinates of the dot, and it needs to
// be drawn somewhere between p3 and p4 (ie, p3x < p0x < p4x :
p0y = p3y + (p4y-p3y) * (p0x-p3x) / (p4x-p3x);

Have a look at the Wikipedia page and especially the drawings, if my interpretation of your question is wrong just let me know.

fvu
  • 32,488
  • 6
  • 61
  • 79