0

我计算了一个方向(如果需要,您可以将其称为向量,但它实际上是一个斜率......)并且我想从给定 y 值的函数中获取 x 值。

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

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

我知道这真的是基本的高中代数,但由于某种原因我无法概念化它......

4

4 回答 4

2

如果端点是 A(x1,y1) 和 B(x2,y2),则斜率定义为:

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
于 2012-07-02T12:01:58.003 回答
1

使用Bresenham 的线算法

这里有多种语言的实现包括javascript

于 2012-07-02T11:55:20.423 回答
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;
}
于 2012-07-02T12:04:54.360 回答
1

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

y = y1 + m*(x-x1)  // point is (x,y)
于 2012-07-02T12:45:44.577 回答