void drawedge(int x1, int y1, int x2, int y2, char color)
{
int side = 0;
float temp = x1;
float xslope = (x2-x1)/(y2-y1);
if (y1 >= y2)
{
side = 1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}
for (y=y1;y<=y2;y++)
{
xpos[y][side]=x;
x1 += xslope;
}
}
All we do this time, is filling the dots in the xpos array, instead of the screen. Now, to write the entire polygon in the xpos array we do:
Все мы делаем помещаем точки в xpos массиве, вместо экрана. Теперь, чтобы записать весь многоугольник в xpos массив, мы делаем:
drawedge(x1,y1,x2,y2);
drawedge(x2,y2,x3,y3);
drawedge(x3,y3,x1,y1);
Now, we should have written the whole polygon in the xpos array. But we still have to write this, and now we se the bonus about this way of drawing polygons. It appears we can write the polygon by drawing a single line from xpos[y][0] to xpos[y][1]. If you look at figure four, this is actually correct. If we draw from xpos[2][0] to xpos[2][1] in figure four, and continue this until max_y, we actually will get a filled polygon. In mcga mode we dont have to borther about bankswitch's and therefore we can make our line routine very fast. It can look like this in watcom C:
Теперь, мы должны нарисовать целый многоугольник из xpos массива. Но мы все еще должны нарисовать это, и теперь мы видим премию для этого способа рисования многоугольников. Оказывается, что мы можем рисовать многоугольник, рисуя одну линию из xpos [y] [0] до xpos [y] [1]. Если Вы посмотрите на рисунок четыре, то это фактически правильно. Если мы рисуем из xpos [2] [0] до xpos [2] [1] на рисунке четыре, и продолжаем это до max_y, мы фактически получим заполненный многоугольник. В mcga ( 320 x 200 x 256 ) режиме мы не должны относительно переключения банков, и следовательно мы можем делать нашу подпрограмму рисования строки очень быстро. На watcom C:
void hline(int x1,int x2,int y,char color)
{
int i;
int wherey;
if (x1>=x2)
{
i=x1;
x1=x2;
x2=i;
}
wherey=y*320;
for (i=x1;i<=x2;i++)
*(char*)(0xa0000+(x)+((wherey)*320))=color;
}
This way we dont have to do the multiply for every dot, as we would if we choosed to make a polygon routine that used ypos instead of xpos. But what do we need to make now? Yes, lets make this routine complete, ready to implement. What do we need then? At first we need the main routine, lets call it polygon(int x1,int y1,int x2,int y2,int x3,int y3, int color); and we some basic in it like making sure x and y doesnt exseed their highest value and finding the minimun and maximun y value. The entire routine would look like this:
С этим способом мы не должны делать умножение для каждой точки, поскольку мы выбрали подпрограмму многоугольника, которая использует ypos вместо xpos. Но что мы должны делать теперь? Да, давайте сделаем эту процедуру полной и готовой для выполнения. В чем мы нуждаемся тогда? Сначала, мы нуждаемся в основной подпрограмме, давайте назовем ее polygon(int x1, int y1, int x2, int y2, int x3, int y3, int color). Вначеле ее мы вычислим min_y и max_y. Вся подпрограмма выглядела следующим образом:
void drawedge(int x1, int y1, int x2, int y2)
{
int side = 0;
float temp = x1;
float xslope = (x2-x1)/(y2-y1);
if (y1 >= y2)
{
side = 1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}
for (y=y1;y<=y2;y++)
{
xedge[y][side] = x1;
x1 += xslope;
}
}
void hline(int x1,int x2,int y,char color)
{
int i;
int wherey;
if (x1>=x2)
{
i=x1;
x1=x2;
x2=i;
}
wherey=y*320;
for (i=x1;i<=x2;i++)
*(char*)(0xa0000+(x+wherey)=color;
}
void polygon(int x1,int y1,int x2,int y2,int x3,int y3,char c)
{
int minx,maxx;
drawedge(x1,y1,x2,y2);
drawedge(x2,y2,x3,y3);
drawedge(x3,y3,x1,y1);
miny=y1;
if (miny > y2)
miny=y2;
if (miny > y3)
miny=y3;
maxy=y1;
if (maxy < y2)
maxy=y2;
if (maxy < y3)
maxy=y3;
minx=x1;
if (minx > x2)
minx=x2;
if (minx > x3)
minx=x3;
maxx=x1;
if (maxx < x2)
maxx=x2;
if (maxx < x3)
maxx=x3;
if (maxy==miny)
hline(minx,maxx,miny,c);
else
for (y=miny;y<=maxy;y++)
hline(xedge[y][0],xedge[y][1],y,c);
};
This should do the trick, and works pretty good. Happy coding!
Вот это трюк, и работает он довольно хорошо. Счастливого кодирования!
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.