开发者

How do I determine the points in a line, in order, from P1 to P2?

开发者 https://www.devze.com 2022-12-19 04:34 出处:网络
Hey guys. I\'m trying to make a simple game for Android in which the player drags a space ship with one thumb and fires at enemy ships by tapping empty space with the other thumb. Thing is, to animate

Hey guys. I'm trying to make a simple game for Android in which the player drags a space ship with one thumb and fires at enemy ships by tapping empty space with the other thumb. Thing is, to animate the bullets traveling across space, I need the list of points between the user's spaceship, and the end of the screen, the line passing through the place the user tapped.

I know I have to parametrize the line, and have read some about the subject, but I can't quite grasp it, especially on how to convert it to code. The way I see it, I need to convert the line segment between the user's ship (P1) and the place the user taps (P2) the smallest possible piece, the rate of change between x and y, the slope, I guess. Once I have that I can just multiply the rate of change by the distance from P1 to get the desired point.

But it ain't quite working. Any help would be appreciated. Thanks.

EDIT: This is what I was doing. This is all to calculate the rate of change, which I use on the Beam's update method to update it's position by multiplying it by speed...

Beam(Renderer r, float OX, float OY, float TX, float TY)
{
    super(r);

    p.x = (int) OX;
    p.y = (int) OY;

    paint = new Paint();
    paint.setColor(Color.BLUE);


    float X = TX - OX;
开发者_如何学JAVA    float Y = TY - OY;

    boolean xPositive = X >= 0;
    boolean yPositive = Y >= 0;

    if(X < Y)
    {
        RateOfChangeX = Math.abs(X) / Math.abs(Y);
        RateOfChangeY = 1;
    }
    else
    {
        RateOfChangeX = 1;
        RateOfChangeY = Math.abs(Y) / Math.abs(X);
    }
    if(RateOfChangeX < 0 & xPositive)
    {
        RateOfChangeX = RateOfChangeX * -1;
    }
    if(RateOfChangeY < 0 & yPositive)
    {
        RateOfChangeY = RateOfChangeY * -1;
    }
}


This is an example of how to interpolate which points lie between two points. The code to draw a line is very different and much simpler

This example is in C, but the code may be useful. im is the image, stride is the stride of the image, sx and sy are the amount you are moving up/down or left/right.

float x=sx, y=sy;
  for ( int i=1; i < int(totalLength); i++ ) { 
    int  top = stride*int(y)    + int(x),
         bot = stride*int(y+1)  + int(x);
    float xr = x-int(x),
          xl = 1-xr,
          yt = y-int(y),
          yb = 1-yt;
    newLine[i]= im[top]*xr*yt   +
                im[top-1]*xl*yt + 
                im[bot]*xr*yb   +
                im[bot-1]*xl*yb;
    x+=xgain;
    y+=ygain;
  }

Getting sx/sy (x1,y1) (x2,y2) are the points you are drawing between... Code is python:

totalLength = math.sqrt(math.pow(x2-x1,2) + math.pow(y2-y1,2))

if x1 < x2:
  sx,sy,ex,ey=(x1,y1,x2,y2)
else:
  sx,sy,ex,ey=(x2,y2,x1,y1)

xgain = float(ex-sx)/totalLength
ygain = float(ey-sy)/totalLength

The point to note is that you need to figure out the x and y movement for each step, and then interpolate as shown. To think about it, just write a grid on a piece of paper, and look where the dot should be for each step. Draw a 1px by 1px box around that point.

This algorithm simplifies slightly by drawing a 1px by 1px box backwards from that point, using interpolation between the four grid points touched by that box. Note that the first pixel is skipped as it is a special case. The first pixel represents a problem as it could be outside the image, and you would need to check that you could make the computation before actually performing the computation.


Basically you can use Slope–intercept form of a linear equation. To do this without using floating point arithmetic you might use on of these algorithms:

  • DDA Line Drawing Algorithm which I would recommend. There is also a code snippet.
  • Or Bresenham Line-Drawing Algorithms


Bezier Curve is the best way to do this.....link for this

http://en.wikipedia.org/wiki/B%C3%A9zier_curve

0

精彩评论

暂无评论...
验证码 换一张
取 消