浮点型坐标 画线

sdlsl 2010-09-01 02:14:20
如题,若我的两个坐标都为浮点型,怎样利用这浮点型坐标来确定画线时 它的真实坐标呢?求高手指点迷津!
...全文
440 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
liufang8318 2010-09-05
  • 打赏
  • 举报
回复
VC中的绘图好像都是整型值吧,没有浮点值。CPoint()里面都是整型值。
sl_shalou 2010-09-05
  • 打赏
  • 举报
回复
学习学习
zhoujk 2010-09-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sdlsl 的回复:]
引用 3 楼 zhoujk 的回复:
图像的坐标点只能是整数,因此只能取整以后进行运算。为了保证后续运算的精度,可以将显示用的数据和实际运算的数据分离


"可以将显示用的数据和实际运算的数据分离" ? 能不能说的具体些?
[/Quote]
绘图使用整形坐标,后台的其它运算使用浮点坐标。
dzy198239 2010-09-02
  • 打赏
  • 举报
回复
我看了楼主的上一个帖子,楼主原来就是为了画粗线呀。我把Wu算法改了一下,不再有抗锯齿的能力,只是用来画粗线。
void DrawThickLine( CDC *pDC, int X0, int Y0, int X1, int Y1, COLORREF clrLine )
{
/* Make sure the line runs top to bottom */
if (Y0 > Y1)
{
int Temp = Y0; Y0 = Y1; Y1 = Temp;
Temp = X0; X0 = X1; X1 = Temp;
}

/* Draw the initial pixel, which is always exactly intersected by
the line and so needs no weighting */
pDC->SetPixel( X0, Y0, clrLine );
pDC->SetPixel(X0+XDir,Y0,clrLine);
pDC->SetPixel(X0+2*XDir,Y0,clrLine);
pDC->SetPixel(X0,Y0+1,clrLine);
pDC->SetPixel(X0,Y0+2,clrLine);

int XDir, DeltaX = X1 - X0;
if( DeltaX >= 0 )
{
XDir = 1;
}
else
{
XDir = -1;
DeltaX = 0 - DeltaX; /* make DeltaX positive */
}

/* Special-case horizontal, vertical, and diagonal lines, which
require no weighting because they go right through the center of
every pixel */
int DeltaY = Y1 - Y0;
if (DeltaY == 0)
{
/* Horizontal line */
while (DeltaX-- != 0)
{
X0 += XDir;
pDC->SetPixel( X0, Y0, clrLine );
pDC->SetPixel(X0,Y0+1,clrLine);
pDC->SetPixel(X0,Y0+2,clrLine);
/*这是一个宽为三个像素的的直线,如果你想加粗,继续pDC->SetPixel()就可以了。*/
}
return;
}
if (DeltaX == 0)
{
/* Vertical line */
do
{
Y0++;
pDC->SetPixel( X0, Y0, clrLine );
pDC->SetPixel(X0+XDir,Y0,clrLine);
pDC->SetPixel(X0+2*XDir,Y0,clrLine);
/*这是一个宽为三个像素的的直线,如果你想加粗,继续pDC->SetPixel()就可以了。*/
} while (--DeltaY != 0);
return;
}

if (DeltaX == DeltaY)
{
/* Diagonal line */
do
{
X0 += XDir;
Y0++;
pDC->SetPixel( X0, Y0, clrLine );
pDC->SetPixel(X0+XDir,Y0,clrLine);
pDC->SetPixel(X0+2*XDir,Y0,clrLine);
/*这是一个宽为三个像素的的直线,如果你想加粗,继续pDC->SetPixel()就可以了。*/
} while (--DeltaY != 0);
return;
}

unsigned short ErrorAdj;
unsigned short ErrorAccTemp;

/* Line is not horizontal, diagonal, or vertical */
unsigned short ErrorAcc = 0; /* initialize the line error accumulator to 0 */



/* Is this an X-major or Y-major line? */
if (DeltaY > DeltaX)
{
/* Y-major line; calculate 16-bit fixed-point fractional part of a
pixel that X advances each time Y advances 1 pixel, truncating the
result so that we won't overrun the endpoint along the X axis */
ErrorAdj = ((unsigned long) DeltaX << 16) / (unsigned long) DeltaY;
/* Draw all pixels other than the first and last */
while (--DeltaY) {
ErrorAccTemp = ErrorAcc; /* remember currrent accumulated error */
ErrorAcc += ErrorAdj; /* calculate error for next pixel */
if (ErrorAcc <= ErrorAccTemp) {
/* The error accumulator turned over, so advance the X coord */
X0 += XDir;
}
Y0++; /* Y-major, so always advance Y */

pDC->SetPixel( X0, Y0, clrLine );
pDC->SetPixel( X0 + XDir, Y0, clrLine );
pDC->SetPixel( X0+2*XDir, Y0, clrLine);
/*这是一个宽为三个像素的的直线,如果你想加粗,继续pDC->SetPixel()就可以了。*/
}
/* Draw the final pixel, which is always exactly intersected by the line
and so needs no weighting */
pDC->SetPixel( X1, Y1, clrLine );
pDC->SetPixel( X1+XDir,Y1,clrLine);
pDC->SetPixel(X1+2*XDir,Y1,clrLine);
return;
}
/* It's an X-major line; calculate 16-bit fixed-point fractional part of a
pixel that Y advances each time X advances 1 pixel, truncating the
result to avoid overrunning the endpoint along the X axis */
ErrorAdj = ((unsigned long) DeltaY << 16) / (unsigned long) DeltaX;
/* Draw all pixels other than the first and last */
while (--DeltaX) {
ErrorAccTemp = ErrorAcc; /* remember currrent accumulated error */
ErrorAcc += ErrorAdj; /* calculate error for next pixel */
if (ErrorAcc <= ErrorAccTemp) {
/* The error accumulator turned over, so advance the Y coord */
Y0++;
}
X0 += XDir; /* X-major, so always advance X */
/* The IntensityBits most significant bits of ErrorAcc give us the
intensity weighting for this pixel, and the complement of the
weighting for the paired pixel */

pDC->SetPixel( X0, Y0, clrLine );
pDC->SetPixel( X0, Y0 + 1, clrLine );
pDC->SetPixel( X0, Y0+2, clrLine);
/*这是一个宽为三个像素的的直线,如果你想加粗,继续pDC->SetPixel()就可以了。*/
}

/* Draw the final pixel, which is always exactly intersected by the line
and so needs no weighting */
pDC->SetPixel( X1, Y1, clrLine );
pDC->SetPixel(X1,Y1+1,clrLine);
pDC->SetPixel(X1,Y1+2,clrLine);

}
fandh 2010-09-01
  • 打赏
  • 举报
回复
你干脆学微软得了!
还有就是,在201,202画的色彩,根据浮点数离1的比例,将色彩弱化。。。
m_tornado 2010-09-01
  • 打赏
  • 举报
回复
截断又不是四舍五入,除非你自己去处理~
sdlsl 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fandh 的回复:]
可以这样,只要有浮点数也表示一个点,这样,根据浮点数的大小(仅仅是小数点)来淡化实际画的点!这样感觉会好点!
[/Quote]

怎么根据小数部分来淡化,能说具体点吗? 刚才我试了下 只用SetPixel()方法来打点,如pDC->SetPixel(200.9994,201.9562,RGB(255,0,0)) 来打点 理论上这个点应该距离(201,202)最近,应该取这点啊,可是微软画出来的却是(200,201)这个点,也就是说都把小数部分给舍弃了!
fandh 2010-09-01
  • 打赏
  • 举报
回复
可以这样,只要有浮点数也表示一个点,这样,根据浮点数的大小(仅仅是小数点)来淡化实际画的点!这样感觉会好点!
sdlsl 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhoujk 的回复:]
图像的坐标点只能是整数,因此只能取整以后进行运算。为了保证后续运算的精度,可以将显示用的数据和实际运算的数据分离
[/Quote]

"可以将显示用的数据和实际运算的数据分离" ? 能不能说的具体些?
fandh 2010-09-01
  • 打赏
  • 举报
回复
那你有没有看到微软画出来是什么结果?比较一下,然后找规律!
zhoujk 2010-09-01
  • 打赏
  • 举报
回复
图像的坐标点只能是整数,因此只能取整以后进行运算。为了保证后续运算的精度,可以将显示用的数据和实际运算的数据分离
sdlsl 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fandh 的回复:]
首先你的浮点型坐标来自那儿?表示什么意思?
实际上就是一个坐标转换!
[/Quote]
还是接着我上次提的那个问题的引伸,利用矩形画宽线时,不是先得求出四边形的四个顶点吗,这样求出来的顶点坐标就是浮点型的,而像素是整形的,如下图,怎么确定此点应该取正方形的哪个顶点。

_
|.|
ˉ
fandh 2010-09-01
  • 打赏
  • 举报
回复
首先你的浮点型坐标来自那儿?表示什么意思?
实际上就是一个坐标转换!

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧