2013-02-02 35 views
0

所以我有4点,P1,P2,P3和P4。当它们到达太远时,它们不断移动到左边并且移回到最右边(窗口宽度+ 100)(x-100)。他们的Y是随机的。Java - 使对象沿着划线

我还在每个点之间绘制了线条,这使得流畅的“锯齿状”线条在屏幕上移动(类似于Windows任务管理器中的CPU使用情况图表)。

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); 
    } 

我想要一个点在窗口中有一个常量X,但是在Y轴上移动这条线,我该怎么做?

+1

我真的不明白你在问什么。请提供更多信息或插图。 – atomman

回答

2

我想你可能会寻找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); 

看一看维基百科页面,尤其是图纸,如果我对你的问题的解释是错误的只是让我知道。