i got totally lame clip line function
(its old i see how bad names i use thise times)
int ClipLineToArea(int* xi, int* yi, int* xi_, int* yi_, int left, int up, int right, int down)
{
int x = *xi;
int y = *yi;
int x_ = *xi_;
int y_ = *yi_;
// int up = 0 ;
// int down = frame_size_y - 1;
//
// int left = 0 ;
// int right = frame_size_x - 1;
if(x==x_) //pionowa linia
{
if(x>=left && x<=right)
{
if(y<y_)
{
if(y< up && y_>=up) y=up;
if(y<=down && y_> down) y_=down;
}
if(y_<y)
{
if(y_< up && y>=up) y_=up;
if(y_<=down && y> down) y=down;
}
// ERROR_("clip pionowa");
(*xi) = x;
(*yi) = y;
(*xi_) = x_;
(*yi_) = y_;
return 1;
}
else
{
// ERROR_("pionowa linia poza zakresem");
return 0;
}
}
if(y==y_) ///pozioma
{
if(y>=up && y<=down)
{
if(x<x_)
{
if(x<left && x_>=left) x=left;
if(x<=right && x_>right) x_=right;
}
if(x_<x)
{
if(x_<left && x>=left) x_=up;
if(x_<=right && x>right) x=right;
}
//ERROR_("clip pozioma");
(*xi) = x;
(*yi) = y;
(*xi_) = x_;
(*yi_) = y_;
return 1;
}
else
{
// ERROR_("p0zioma linia poza zakresem");
return 0;
}
}
// return;
/*
float x = *xi;
float y = *yi;
float x_ = *xi_;
float y_ = *yi_;
float up = 0 ;
float down = CLIENT_Y - 1;
float left = 0 ;
float right = CLIENT_X - 1;
*/
float DYdoDX = float(y_ - y) / float( x_ - x);
float DXdoDY = float(x_ - x) / float( y_ - y);
float a = 0.5 ;
if(y < y_)
{
if(y<up && y_>=up)
{
x = x + (((up-y-a)*DXdoDY));
y = up;
}
if(y<=down && y_>down)
{
x_ = x_ + (((down-y_+a)*DXdoDY));
y_ = down;
}
}
else if(y_ < y)
{
if(y_<up && y>=up)
{
x_ = x_ + (((up-y_-a)*DXdoDY));
y_ = up;
}
if(y_<=down && y>down)
{
x = x + (((down-y+a)*DXdoDY));
y = down;
}
}
//clip to bottom
// clip left
if(x < x_)
{
if(x<left && x_>=left)
{
y = y + (((left-x-a)*DYdoDX));
x = left;
}
if(x<=right && x_>right)
{ // 33 499 - 0
y_ = y_ + (((right-x_+a)*DYdoDX));
x_ = right;
}
}
else if(x_ < x)
{
if(x_<left && x>=left)
{
y_ = y_ + (((left-x_-a)*DYdoDX));
x_ = left;
}
if(x_<=right && x>right)
{
y = y + (((right-x+a)*DYdoDX));
x = right;
}
}
if(x<left) return 0;
if(x>right) return 0;
if(y<up) return 0;
if(y>down) return 0;
if(x_<left) return 0;
if(x_>right) return 0;
if(y_<up) return 0;
if(y_>down) return 0;
*xi = x;
*yi = y;
*xi_ = x_;
*yi_ = y_;
return 1;
}
and im not even sure it this works ok,
with some tests on random generated lines it seems to work ok hovever - hovever it has those errors i said i mean clip+bresenham has slight different pixel positioning that bressenhams clipped on pixels
this above is also not quite efficient i think hovever when measured and compered to drawline routines that make clip on sperate pixels it speed up things, how much depends mostly obviously on the area of offscreen pixels, sometomes it speeds things from 4 to 3 and sometimes from 40 to 10
must say that form some reasons such clipline routines are distaster for me to vrite, hovever i later should revrite it nicer and improve it (as i said im still not sure if this generally works well or not quite)