Max Hudson
Max Hudson

Reputation: 10226

draw line to y value at angle

I calculated a direction (you can call it a vector if you want, but it's really a slope...) and I want to get an x value from a function given a y value.

Basically, I am trying to draw a line from an x, y value to an x, y value and I have the direction.

Say, given a slope/direction of 1/4 (rise over run), a starting point of 200, and an ending y value of 250, how would I find x?

I know this is really basic highschool algebra, but for some reason I can't conceptualize it...

Upvotes: 0

Views: 436

Answers (4)

John Alexiou
John Alexiou

Reputation: 29274

Given a point (x1,y1) and a slope m then any other point on the line is given by

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

Upvotes: 1

Šime Vidas
Šime Vidas

Reputation: 186103

If the end points are A(x1,y1) and B(x2,y2), the slope is defined as:

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

Since you do have the slope, you need to have at least three coordinates in order to be able to compute the remaining one.

From your question I assume you want to compute y2. Therefore, you need to have x1, y1, and x2.

Example:

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

Upvotes: 2

Ogoun Er
Ogoun Er

Reputation: 11

Bresenham's line algorithm:

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

Upvotes: 1

Related Questions