以角度绘制线到y值

时间:2012-07-02 11:52:28

标签: javascript math

我计算了一个方向(你可以把它称为向量,但它实际上是一个斜率......)我想从给定y值的函数中得到一个x值。

基本上,我试图从x,y值绘制一条线到x,y值,我有方向。

假设,如果斜率/方向为1/4(上升超过运行),起点为200,结束y值为250,我将如何找到x?

我知道这是非常基本的高中代数,但出于某种原因我无法将其概念化......

4 个答案:

答案 0 :(得分:2)

如果终点是A(x1,y1)和B(x2,y2),则slope定义为:

m = ( y2 - y1 ) / ( x2 - x1 )

由于你有斜率,你需要至少有三个坐标才能计算剩下的坐标。

从你的问题我假设你想要计算y2。因此,您需要拥有x1,y1和x2。

示例:

m = 1/4
A(1,1)
B(9,y2)
---
y2 = ?

m = ( y2 - y1 ) / ( x2 - x1 ) 
y2 - y1 = m * ( x2 - x1 )
y2 = m * ( x2 - x1 ) + y1
y2 = 1/4 * ( 9 - 1 ) + 1
y2 = 3

答案 1 :(得分:1)

答案 2 :(得分:1)

Bresenham的线算法:

void DrawLineLCD(int x1,int y1,int x2,int y2,int nState)
{
    unsigned int nTmp;
    unsigned int nAlt=0;
    int x,y;        // where is the current pixel.
    int dx;     // dx is the delta for x
    int dy;     // dy is the delta for y
    int StepVal=0;  // variable for figuring out when to increment the other 
axis.
    if (x1>x2 && y1>y2)
    {
        nTmp=x2;
        x2=x1;
        x1=nTmp;

        nTmp=y2;
        y2=y1;
        y1=nTmp;

        dx=x2-x1;   // dx is the delta for x
        dy=y2-y1;   // dy is the delta for y
    }else
    {
        dx=x2-x1;   // dx is the delta for x
        dy=y2-y1;   // dy is the delta for y

        if (dy<0)
        {   
            dy=-dy;

            nTmp=y2;
            y2=y1;
            y1=nTmp;

            nAlt=1;
        }else
            if (dx<0)
            {   
                dx=-dx;

                nTmp=x2;
                x2=x1;
                x1=nTmp;

                nAlt=1;
            }
    }

    if (nAlt)
    {

        if(dx>=dy)       // The slope is less than 45 degres
        {
            y=y2;
            for(x=x1; x<=x2; x++)
            {
                // Call your function to draw a pixel here.
                SetPixelLCD(x,y,nState);
                StepVal+=dy;
                if(StepVal>=dx) // Increment y if enough x steps
                                // have been taken.
                {
                    y--;
                    StepVal-=dx;    // Reset StepVal, but  
                    // not to 0.  This gives even slopes.
                }
            }
        }
        else    // The slope is greater than 45 degrees, just like 
                // above, but with y instead of x.
        {
            x=x2;
            for(y=y1; y<=y2; y++)
            {
                // Call your function to draw a pixel here.
                SetPixelLCD(x,y,nState);
                StepVal+=dx;
                if(StepVal>=dy)
                {
                    x--;
                    StepVal-=dy;
                }
            }
        }
        return;
    }

    if(dx>=dy)       // The slope is less than 45 degres
    {
        y=y1;
        for(x=x1; x<=x2; x++)
        {
            // Call your function to draw a pixel here.
            SetPixelLCD(x,y,nState); 
            StepVal+=dy;
            if(StepVal>=dx) // Increment y if enough x steps
                            // have been taken.
            {
                y++;
                StepVal-=dx;    // Reset StepVal, but  
                // not to 0.  This gives even slopes.
            }
        }
    }
    else    // The slope is greater than 45 degrees, just like 
            // above, but with y instead of x.
    {
        x=x1;
        for(y=y1; y<=y2; y++)
        {
            // Call your function to draw a pixel here.
            SetPixelLCD(x,y,nState);
            StepVal+=dx;
            if(StepVal>=dy)
            {
                x++;
                StepVal-=dy;
            }
        }
    }
    return;
}

答案 3 :(得分:1)

给定一个点(x1,y1)和一个斜率m,那么该线上的任何其他点都由

给出
y = y1 + m*(x-x1)  // point is (x,y)