Javascript 以角度将线绘制为y值
我计算了一个方向(如果你愿意,你可以称它为向量,但它实际上是一个斜率…),我想从给定y值的函数中得到一个x值 基本上,我试图画一条从x,y值到x,y值的线,我有方向 假设坡度/方向为1/4(上升超过行程),起点为200,终点y值为250,如何找到x 我知道这确实是高中的基础代数,但由于某些原因,我无法将其概念化…使用 这里有(包括)使用说明 这里是(包括)如果端点是A(x1,y1)和B(x2,y2),则定义为:Javascript 以角度将线绘制为y值,javascript,math,Javascript,Math,我计算了一个方向(如果你愿意,你可以称它为向量,但它实际上是一个斜率…),我想从给定y值的函数中得到一个x值 基本上,我试图画一条从x,y值到x,y值的线,我有方向 假设坡度/方向为1/4(上升超过行程),起点为200,终点y值为250,如何找到x 我知道这确实是高中的基础代数,但由于某些原因,我无法将其概念化…使用 这里有(包括)使用说明 这里是(包括)如果端点是A(x1,y1)和B(x2,y2),则定义为: m = ( y2 - y1 ) / ( x2 - x1 ) 因为你有坡度,你需要至
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
如果端点为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
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;
}
void DrawLineLCD(intx1、inty1、intx2、inty2、intnstate)
{
无符号整数nTmp;
无符号整数nAlt=0;
int x,y;//其中是当前像素。
int dx;//dx是x的增量
int dy;//dy是y的增量
int-StepVal=0;//用于计算何时递增另一个变量的变量
轴
如果(x1>x2&&y1>y2)
{
nTmp=x2;
x2=x1;
x1=nTmp;
nTmp=y2;
y2=y1;
y1=nTmp;
dx=x2-x1;//dx是x的增量
dy=y2-y1;//dy是y的增量
}否则
{
dx=x2-x1;//dx是x的增量
dy=y2-y1;//dy是y的增量
如果(dy=dy)//斜率小于45度
{
y=y1;
对于(x=x1;x=dx)//如果有足够的x步,则增加y
//已经被带走了。
{
y++;
StepVal-=dx;//重置StepVal,但是
//不为0。这将提供均匀的坡度。
}
}
}
否则//坡度大于45度,就像
//上面,但用y代替x。
{
x=x1;
对于(y=y1;y=dy)
{
x++;
StepVal-=dy;
}
}
}
返回;
}
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;
}
void DrawLineLCD(intx1、inty1、intx2、inty2、intnstate)
{
无符号整数nTmp;
无符号整数nAlt=0;
int x,y;//其中是当前像素。
int dx;//dx是x的增量
int dy;//dy是y的增量
int-StepVal=0;//用于计算何时递增另一个变量的变量
轴
如果(x1>x2&&y1>y2)
{
nTmp=x2;
x2=x1;
x1=nTmp;
nTmp=y2;
y2=y1;
y1=nTmp;
dx=x2-x1;//dx是x的增量
dy=y2-y1;//dy是y的增量
}否则
{
dx=x2-x1;//dx是x的增量
dy=y2-y1;//dy是y的增量
如果(dy=dy)//斜率小于45度
{
y=y1;
对于(x=x1;x=dx)//如果有足够的x步,则增加y
//已经被带走了。
{
y++;
StepVal-=dx;//重置StepVal,但是
//不为0。这将提供均匀的坡度。
}
}
}
否则//坡度大于45度,就像
//上面,但用y代替x。
{
x=x1;
对于(y=y1;y=dy)
{
x++;
StepVal-=dy;
}
}
}
返回;
}
给定一个点(x1,y1)
和一个斜率m
,则直线上的任何其他点由
y = y1 + m*(x-x1) // point is (x,y)
给定一个点(x1,y1)
和一个斜率m
,则直线上的任何其他点由
y = y1 + m*(x-x1) // point is (x,y)
200的起点,即x或y?200的起点,即x或y?