* that pixel coordinates increase in the right and down directions (e.g. that the pixel at (1,1) is directly above the pixel at (1,2)), and
* that the pixel centers have integer coordinates.
The endpoints of the line are the pixels at (x0, y0) and (x1, y1), where the first coordinate of the pair is the column and the second is the row.
The algorithm will be initially presented only for the octant in which the segment goes down and to the right (x0‚â§x1 and y0‚â§y1), and its horizontal projection x1 ‚àí x0 is longer than the vertical projection y1 ‚àí y0 (the line has a slope whose absolute value is less than 1 and greater than 0.) In this octant, for each column x between x0 and x1, there is exactly one row y (computed by the algorithm) containing a pixel of the line, while each row between y0 and y1 may contain multiple rasterized pixels.
Bresenham's algorithm chooses the integer y corresponding to the pixel center that is closest to the ideal (fractional) y for the same x; on successive columns y can remain the same or increase by 1. The general equation of the line through the endpoints is given by:
\frac{y - y_0}{y_1-y_0} = \frac{x-x_0}{x_1-x_0}.
#include<stdio.h>
#include<graphics.h>
void ikan(int,int,int,int);
void setPixel(int,int);
void main()
{
int x1,y1,x2,y2;
int gd=DETECT,gm=0;
initgraph(&gd,&gm,0);
printf(“Masukkan koordinat titik x1 : \n “);
scanf(“%d”,&x1);
printf(“Masukkan koordinat titik y1: \n “);
scanf(“%d”,&y1);
printf(“Masukkan koordinat titik x2: \n”);
scanf(“%d”,&x2);
printf(“Masukkan koordinat titik y2: \n “);
scanf(“%d”,&y2);
initgraph(&gd,&gm,0);
ikan(x1,y1,x2,y2);
getch();
closegraph();
}
void ikan(int x1,int y1,int x2,int y2)
{
int x,y;
int dx, dy;
int d;
dy=y2-y1;
dx=x2-x1;
d = dx – 2 * dy;
y = y1;
for (x = x1; x <= x2; x++)
{
putpixel(x,y,4);
if (d < 0)
{
y = y +1;
d = d + 2*dx – 2*dy;
}
else
{
d = d – 2*dy;
}
}
}
void setPixel(int x,int y)
{
putpixel(x,y,4);
}
Tidak ada komentar:
Posting Komentar