You are on page 1of 7

Representations for Lines and Curves

[Hill: p. 555-560] A preliminary step to drawing lines and curves is choosing a suitable representation for them. There are three possible choices which are potentially useful.
Explicit: y = f(x)

line circle
Parametric: x = f(t), y = f(t)

line

circle

Implicit: f(x,y) = 0

line

circle

Optimal Line Drawing

Drawing lines on a raster grid implicitly involves approximation. The general process is called rasterization or scan-conversion. What is the best way to draw a line from the pixel (x1,y1) to (x2,y2)? Such a line should ideally have the following properties.
y y y y y y y

straight pass through endpoints smooth independent of endpoint order uniform brightness brightness independent of slope efficient

A Straightforward Implementation DrawLine(x1,y1,x2,y2) int x1,y1,x2,y2; { float y; int x; for (x=x1; x<=x2; x++) { y = y1 + (x-x1)*(y2-y1)/(x2-x1) SetPixel(x, Round(y) ); } } A Better Implementation DrawLine(x1,y1,x2,y2) int x1,y1,x2,y2; { float m,y; int dx,dy,x; dx = x2 - x1; dy = y2 - y1; m = dy/dx; y = y1 + 0.5; for (x=x1; x<=x2; x++) { SetPixel(x, Floor(y) ); y = y + m; } }

The Midpoint Algorithm

The midpoint algorithm is even better than the above algorithm in that it uses only integer calculations. It treats line drawing as a sequence of decisions. For each pixel that is drawn, the next pixel will be either E or NE, as shown below.

The midpoint algorithm makes use of the the implicit definition of the line, F(x,y) = 0. The E/NE decisions are made as follows.

define

if E is chosen,

if NE is chosen,

The process repeats, stepping along x, making decisions whether to set E or NE pixel.

Initialization

Now we would like an integer-only algorithm. Define

Midpoint Algorithm

The following algorithm produces the desired results for lines having x1 less than x2 and a slope less or equal to 1.
drawline(x1, y1, x2, y2, colour) int x1, y1, x2, y2, colour; { int dx, dy, d, incE, incNE, x, y; dx = x2 - x1; dy = y2 - y1; d = 2*dy - dx; incE = 2*dy; incNE = 2*(dy - dx); y = y1; for (x=x1; x<=x2; x++) { setpixel(x, y, colour); if (d>0) { d = d + incNE; y = y + 1; } else { d = d + incE; } } }

#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x,y,r; void Drawcircle(int,int,int); printf("Enter the Mid points and Radious:"); scanf("%d%d%d",&x,&y,&r); initgraph(&gd,&gm,""); Drawcircle(x,y,r); getch(); closegraph(); } void Drawcircle(int x1,int y1,int r) { int x=0,y=r,p=1-r; void cliplot(int,int,int,int); cliplot(x1,y1,x,y); while(x<y) { x++; if(p<0) p+=2*x+1; else { y--; p+=2*(x-y)+1; } cliplot(x1,y1,x,y); } } void cliplot(int xctr,int yctr,int x,int y) { putpixel(xctr +x,yctr +y,1); putpixel(xctr -x,yctr +y,1); putpixel(xctr +x,yctr -y,1); putpixel(xctr -x,yctr -y,1); putpixel(xctr +y,yctr +x,1); putpixel(xctr -y,yctr +x,1);

putpixel(xctr +y,yctr -x,1); putpixel(xctr -y,yctr -x,1); getch(); }

Mid-Point Ellipse Drawing Algorithm in C/C++ Program #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <time.h> #include <dos.h> int main(void) { int gd=DETECT,gm; int cenx,ceny; float Pk,a,b,x,y; clrscr(); printf(nn Enter a and b: ); scanf(%f%f,&a,&b); initgraph(&gd,&gm,c:\tc\bgi); cenx=getmaxx()/2; ceny=getmaxy()/2; Pk=b*b-b*a*a+0.25*a*a; x=0; y=b; putpixel(cenx+x,ceny+y,WHITE); putpixel(cenx+x,ceny-y,WHITE); putpixel(cenx-x,ceny+y,WHITE); putpixel(cenx-x,ceny-y,WHITE); while (2*x*b*b <= 2*y*a*a) { if (Pk<0) { x=x+1; y=y; Pk=Pk+2*x*b*b+3*b*b; } else

{ x=x+1; y=y-1; Pk=Pk+2*x*b*b+3*b*b-2*y*a*a+2*a*a; } putpixel(cenx+x,ceny+y,WHITE); putpixel(cenx+x,ceny-y,WHITE); putpixel(cenx-x,ceny+y,WHITE); putpixel(cenx-x,ceny-y,WHITE); delay(40); } Pk=(x+0.5)*(x+0.5)*b*b+(y-1)*(y-1)*a*a-a*a*b*b; putpixel(cenx+x,ceny+y,WHITE); putpixel(cenx+x,ceny-y,WHITE); putpixel(cenx-x,ceny+y,WHITE); putpixel(cenx-x,ceny-y,WHITE); while (y>0) { if (Pk>0) { x=x; y=y-1; Pk=Pk-2*y*a*a+3*a*a; } else { x=x+1; y=y-1; Pk=Pk-2*y*a*a+3*a*a+2*x*b*b+2*b*b; } putpixel(cenx+x,ceny+y,WHITE); putpixel(cenx+x,ceny-y,WHITE); putpixel(cenx-x,ceny+y,WHITE); putpixel(cenx-x,ceny-y,WHITE); delay(40); } gotoxy(1,25); printf (npress any key to exit.); getch(); closegraph(); return 0; }

You might also like