If you draw line with one point is outside the screen, pocketfrog may draw it for different direction
try this:
int x1=226;
int y1=57;
int x2=253;
int y2=-94;
display->DrawLine(x1,y1,x2,y2,Color(0,0,0));
pocketfrog draw the wrong line. it looks likely this is related to x2,y2 are out of screen.
but for points below, x2,y2 are out of screen also, pocketfrog can draw the correct line:
x1= 237; y1= 59
x2= 332; y2= -79
why?
more information, see code below, it may draw the correct line:
- Code:
int x1=229;
int y1=57;
int x2=256;
int y2=-94;
display->DrawLine(x2,y2,x1,y1,Color(0,0,0));
However, if you try this one, it will draw wrong line, the only different is x2=255
- Code:
int x1=229;
int y1=57;
int x2=255;
int y2=-94;
display->DrawLine(x2,y2,x1,y1,Color(0,0,0));
The code below in void Rasterizer::DrawLine( int x1, int y1, int x2, int y2, Pixel color ) is somewhere wrong, but I didn't fix it so far. The key problem is:
- Code:
y = p1.y + (p2.y - p1.y) * (xmax - p1.x) / (p2.x - p1.x);
ok, I fixed this bug after debug PockerFrog code, enjoy the fixed code below
- Code:
void Rasterizer::DrawLine( int x1, int y1, int x2, int y2, Pixel color )
{
// Optimize using HLine / VLine when possible
if (x1 == x2)
{
if (y2 < y1) std::swap( y1, y2 );
DrawVLine( x1, y1, y2 - y1, color );
}
else if (y1 == y2)
{
if (x2 < x1) std::swap( x1, x2 );
DrawHLine( x1, y1, x2 - x1, color );
}
else
{
Point p1( x1, y1 );
Point p2( x2, y2 );
if (!m_clipper.IsEmpty())
{
// Clip using Cohen-Sutherland
int xmin = m_clipper.left;
int ymin = m_clipper.top;
int xmax = m_clipper.right - 1;
int ymax = m_clipper.bottom - 1;
int code1 = GetOutCode( p1 );
int code2 = GetOutCode( p2 );
while (code1 || code2)
{
// Trivial rejection
if (code1 & code2)
return;
int code = (code1) ? code1 : code2;
int x, y;
if (code & 1)
{
// Left edge
y = p1.y + (p2.y - p1.y) * (xmin - p1.x) / (p2.x - p1.x);
x = xmin;
}
else if (code & 2)
{
// Right edge
y = p1.y + (p2.y - p1.y) * (xmax - p1.x) / (p2.x - p1.x);
x = xmax;
}
else if (code & 4)
{
// Top edge
x = p1.x + (p2.x - p1.x) * (ymin - p1.y) / (p2.y - p1.y);
y = ymin;
}
else if (code & 8)
{
// Bottom edge
x = p1.x + (p2.x - p1.x) * (ymax - p1.y) / (p2.y - p1.y);
y = ymax;
}
if (code == code1)
{
p1.Set( x, y );
code1 = GetOutCode( p1 );
}
else
{
p2.Set( x, y );
code2 = GetOutCode( p2 );
}
}
}
// Coordinates transform
Transform( p1 );
Transform( p2 );
// Drawing
m_raster->Line( *m_surface->m_buffer, p1, p2, color );
}
}