SlideShare a Scribd company logo
2
Most read
4
Most read
1 Clipping/fill algorithms
//Graphics Point Clipping C Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int tlx,tly,brx,bry,px,py;
void point_clip()
{ int wxmin,wymin,wxmax,wymax;
wxmin=tlx;
wxmax=brx;
wymin=tly;
wymax=bry;
if(px>=wxmin&&px<=wxmax)
if(py>=wymin&&py<=wymax)
putpixel(px,py,RED);
getch();
closegraph();
}
void main(void)
{ int gd=DETECT,gm,xc,yc,r;
clrscr();
printf("Enter the top left coordinate");
scanf("%d%d",&tlx,&tly);
printf("Enter the bottom right coordinate");
scanf("%d%d",&brx,&bry);
printf("n Enter the point");
scanf("%d%d",&px,&py);
initgraph(&gd,&gm,"c:tcbgi"); /*Sometimes it may be
"C:TCBGI" , It depends machine to mac..*/
setbkcolor(YELLOW);
setcolor(RED);
rectangle(tlx,tly,brx,bry);
point_clip();
}
2 Clipping/fill algorithms
//Sutherland-Hodgeman Polygon clipping Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
3 Clipping/fill algorithms
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
}
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}
void clip(float e,float f,float m)
{
while(e<rx1 e>rx2 f<ry1 f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx1;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
4 Clipping/fill algorithms
{
e+=(ry2-f)/m;
f=ry2;
}
x1[j]=e;
y1[j]=f;
j++;
}
}
OUTPUT
Enter the x1 & y1 coordinates:
X1 : 70
Y1 : 80
Enter the x2 & y2 coordinates:
X2 : 250
Y2 : 280
5 Clipping/fill algorithms
//polygon clipping
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry yminn");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry
ymaxn");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinaten");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinaten");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using
lpk<=qpkn");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
6 Clipping/fill algorithms
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clippingn");
printf("p2=0 line is parallel to right clippingn");
printf("p3=0 line is parallel to bottom clippingn");
printf("p4=0 line is parallel to top clippingn");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejectedn");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:tcbgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
7 Clipping/fill algorithms
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejectedn");
else
8 Clipping/fill algorithms
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
printf("Original line cordinatesn");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %dn",x1,y1,x2,y2);
printf("Windows coordinate are n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d
",wxmin,wymin,wxmax,wymax);
printf("New coordinates are n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 =
%dn",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:TCBGI");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}
}
}
9 Clipping/fill algorithms
//C Program For Oblique projection of a 3D object
#include<stdio.h>
#include<math.h>
#include<graphics.h>
main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
double L1,phi;
a[0][0] = 100; a[1][0] = 100; a[2][0] = 100;
a[0][1] = 200; a[1][1] = 100; a[2][1] = 100;
a[0][2] = 200; a[1][2] = 200; a[2][2] = 100;
a[0][3] = 100; a[1][3] = 200; a[2][3] = 100;
a[0][4] = 100; a[1][4] = 100; a[2][4] = 200;
a[0][5] = 200; a[1][5] = 100; a[2][5] = 200;
a[0][6] = 200; a[1][6] = 200; a[2][6] = 200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = 200;
phi = (double) (3.14*45.0)/180 ;
L1 = 0.5;
par[0][0] = 1; par[0][1] = 0;
par[0][2] = L1*cos(phi); par[0][3] = 0;
10 Clipping/fill algorithms
par[1][0] = 0; par[1][1] = 1;
par[1][2] = L1*sin(phi); par[1][3] = 0;
par[2][0] = 0; par[2][1] = 0;
par[2][2] = 0; par[2][3] = 0;
par[3][0] = 0; par[3][1] = 0;
par[3][2] = 0; par[3][3] = 1;
m=4; n=4; p=8;
for(i=0; i<m; i++)
for(k=0; k<p; k++)
b[i][k] = 0;
for(i=0; i<m; i++)
for(k=0; k<p; k++)
for(j=0; j<n; j++)
b[i][k] += (float)par[i][j] * a[j][k];
detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:tcbgi");
ymax = getmaxy();
/*- front plane display -*/
for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
11 Clipping/fill algorithms
}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1,ymax-y1,x2,ymax-y2);
/*- back plane display -*/
setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1,ymax-y1,x2,ymax-y2);
setcolor(13);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1,ymax-y1,x2,ymax-y2);
}
getch(); getch();
}
12 Clipping/fill algorithms
C Program to implement Boundary Fill Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}
void fill_left(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
13 Clipping/fill algorithms
fill_left(x,y+1);
}
delay(1);
}
void main()
{
int x,y,n,i;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:tcbgi");
/*- draw object -*/
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
/*- set seed point -*/
x = 100; y = 100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
14 Clipping/fill algorithms
//C Program to Implement Flood Fill Algorithm
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
15 Clipping/fill algorithms
}
}
void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:tcbgi");
printf("nntEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("nntEnter the cordinates of polygon :nnn ");
for(i=0;i<n;i++)
{
printf("tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("nntEnter the seed pt. : ");
scanf("%d%d",&x,&y);
cleardevice();
setcolor(WHITE);
for(i=0;i<n;i++) /*- draw poly -*/
{
16 Clipping/fill algorithms
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}
/*SAMPLE INPUT*/
/*Enter the number of edges of polygon 4
X0 Y0 = 50 50
X1 Y1 = 200 50
X2 Y2 = 200 300
X3 Y3 = 50 300
Enter the seed point 100 100*/

More Related Content

DOCX
Java practical
PPTX
Queue Implementation Using Array & Linked List
PPT
Elementary data organisation
PPTX
Circular link list.ppt
PPT
String classes and its methods.20
PPT
GAC DS Priority Queue Presentation 2022.ppt
PPT
Swing and AWT in java
PPT
Heap sort
Java practical
Queue Implementation Using Array & Linked List
Elementary data organisation
Circular link list.ppt
String classes and its methods.20
GAC DS Priority Queue Presentation 2022.ppt
Swing and AWT in java
Heap sort

What's hot (20)

PPTX
Array ADT(Abstract Data Type)|Data Structure
PPT
Arrays searching-sorting
PPTX
Doubly linked list (animated)
PPTX
Huffman Coding Algorithm Presentation
PPTX
Stack using Array
PPTX
Writer Monad for logging execution of functions
PPTX
heap Sort Algorithm
PDF
AD3251-Data Structures Design-Notes-Tree.pdf
PPT
PPT
Infix prefix postfix
PDF
Array data structure
PDF
5 collection framework
PDF
Python lambda functions with filter, map & reduce function
PPTX
Linked list
PPTX
Kruskal & Prim's Algorithm
PPTX
Tree traversal techniques
PPT
Priority queues
PPTX
Function Pointer
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
JavaScript / Web Engineering / Web Development / html + css + js/presentation
Array ADT(Abstract Data Type)|Data Structure
Arrays searching-sorting
Doubly linked list (animated)
Huffman Coding Algorithm Presentation
Stack using Array
Writer Monad for logging execution of functions
heap Sort Algorithm
AD3251-Data Structures Design-Notes-Tree.pdf
Infix prefix postfix
Array data structure
5 collection framework
Python lambda functions with filter, map & reduce function
Linked list
Kruskal & Prim's Algorithm
Tree traversal techniques
Priority queues
Function Pointer
[OOP - Lec 19] Static Member Functions
JavaScript / Web Engineering / Web Development / html + css + js/presentation
Ad

Viewers also liked (10)

PDF
Computer Graphics Programes
DOC
SE Computer, Programming Laboratory(210251) University of Pune
PDF
Basics of Computer graphics lab
PPTX
Graphics Programming in C
PPTX
Introduction to graphics programming in c
DOCX
Computer Graphics Lab File C Programs
DOCX
Computer Graphics Practical
PPT
Lecture 2d point,curve,text,line clipping
PPTX
Developing a research Library position statement on Text and Data Mining in t...
PDF
Writing thesis chapters 1-3 guidelines
Computer Graphics Programes
SE Computer, Programming Laboratory(210251) University of Pune
Basics of Computer graphics lab
Graphics Programming in C
Introduction to graphics programming in c
Computer Graphics Lab File C Programs
Computer Graphics Practical
Lecture 2d point,curve,text,line clipping
Developing a research Library position statement on Text and Data Mining in t...
Writing thesis chapters 1-3 guidelines
Ad

Similar to Graphics point clipping c program (20)

DOCX
Cg my own programs
DOCX
Computer graphics File for Engineers
DOCX
Computer graphics lab assignment
DOCX
C lab manaual
PDF
Computer graphics lab manual
DOC
Computer graphics
DOC
Computer graphics
DOCX
Computer graphics programs in c++
DOC
Numerical Methods in C
PDF
1. Translation program#includestdio.h#includeconio.h#incl.pdf
DOCX
PDF
Assignment on Numerical Method C Code
PDF
Program to reflecta triangle
PDF
2Bytesprog2 course_2014_c9_graph
DOCX
DataStructures notes
DOCX
Runge kutta C programme
DOCX
Numerical Method Assignment
DOC
C tech questions
PDF
VTU Data Structures Lab Manual
DOCX
Program for pyramid
Cg my own programs
Computer graphics File for Engineers
Computer graphics lab assignment
C lab manaual
Computer graphics lab manual
Computer graphics
Computer graphics
Computer graphics programs in c++
Numerical Methods in C
1. Translation program#includestdio.h#includeconio.h#incl.pdf
Assignment on Numerical Method C Code
Program to reflecta triangle
2Bytesprog2 course_2014_c9_graph
DataStructures notes
Runge kutta C programme
Numerical Method Assignment
C tech questions
VTU Data Structures Lab Manual
Program for pyramid

More from Dr.M.Karthika parthasarathy (20)

PDF
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
DOC
Linux Lab Manual.doc
PDF
DOCX
PPTX
Unit 1 Introduction to Artificial Intelligence.pptx
DOCX
Unit I What is Artificial Intelligence.docx
PPTX
Introduction to IoT - Unit II.pptx
PDF
DOCX
Chapter 3 heuristic search techniques
DOCX
Ai mcq chapter 2
PPTX
Introduction to IoT unit II
PPTX
Introduction to IoT - Unit I
PDF
Internet of things Unit 1 one word
DOCX
PPTX
Overview of Deadlock unit 3 part 1
PPTX
Examples in OS synchronization for UG
PPTX
Process Synchronization - Monitors
DOCX
.net progrmming part4
DOCX
.net progrmming part3
DOCX
.net progrmming part1
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
Linux Lab Manual.doc
Unit 1 Introduction to Artificial Intelligence.pptx
Unit I What is Artificial Intelligence.docx
Introduction to IoT - Unit II.pptx
Chapter 3 heuristic search techniques
Ai mcq chapter 2
Introduction to IoT unit II
Introduction to IoT - Unit I
Internet of things Unit 1 one word
Overview of Deadlock unit 3 part 1
Examples in OS synchronization for UG
Process Synchronization - Monitors
.net progrmming part4
.net progrmming part3
.net progrmming part1

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Lesson notes of climatology university.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Sports Quiz easy sports quiz sports quiz
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Lesson notes of climatology university.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPH.pptx obstetrics and gynecology in nursing
102 student loan defaulters named and shamed – Is someone you know on the list?
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Sports Quiz easy sports quiz sports quiz
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Structure & Organelles in detailed.
GDM (1) (1).pptx small presentation for students
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

Graphics point clipping c program

  • 1. 1 Clipping/fill algorithms //Graphics Point Clipping C Program #include<stdio.h> #include<conio.h> #include<graphics.h> int tlx,tly,brx,bry,px,py; void point_clip() { int wxmin,wymin,wxmax,wymax; wxmin=tlx; wxmax=brx; wymin=tly; wymax=bry; if(px>=wxmin&&px<=wxmax) if(py>=wymin&&py<=wymax) putpixel(px,py,RED); getch(); closegraph(); } void main(void) { int gd=DETECT,gm,xc,yc,r; clrscr(); printf("Enter the top left coordinate"); scanf("%d%d",&tlx,&tly); printf("Enter the bottom right coordinate"); scanf("%d%d",&brx,&bry); printf("n Enter the point"); scanf("%d%d",&px,&py); initgraph(&gd,&gm,"c:tcbgi"); /*Sometimes it may be "C:TCBGI" , It depends machine to mac..*/ setbkcolor(YELLOW); setcolor(RED); rectangle(tlx,tly,brx,bry); point_clip(); }
  • 2. 2 Clipping/fill algorithms //Sutherland-Hodgeman Polygon clipping Algorithm #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void clip(float,float,float); int i,j=0,n; int rx1,rx2,ry1,ry2; float x1[8],y1[8]; void main() { int gd=DETECT,gm; int i,n; float x[8],y[8],m; clrscr(); initgraph(&gd,&gm,""); printf("coordinates for rectangle : "); scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2); printf("no. of sides for polygon : "); scanf("%d",&n); printf("coordinates : "); for(i=0;i<n;i++) { scanf("%f%f",&x[i],&y[i]); } cleardevice(); outtextxy(10,10,"Before clipping"); outtextxy(10,470,"Press any key...."); rectangle(rx1,ry1,rx2,ry2); for(i=0;i<n-1;i++) line(x[i],y[i],x[i+1],y[i+1]); line(x[i],y[i],x[0],y[0]); getch(); cleardevice();
  • 3. 3 Clipping/fill algorithms for(i=0;i<n-1;i++) { m=(y[i+1]-y[i])/(x[i+1]-x[i]); clip(x[i],y[i],m); } clip(x[0],y[0],m); outtextxy(10,10,"After clipping"); outtextxy(10,470,"Press any key...."); rectangle(rx1,ry1,rx2,ry2); for(i=0;i<j-1;i++) line(x1[i],y1[i],x1[i+1],y1[i+1]); getch(); } void clip(float e,float f,float m) { while(e<rx1 e>rx2 f<ry1 f>ry2) { if(e<rx1) { f+=m*(rx1-e); e=rx1; } else if(e>rx2) { f+=m*(rx2-e); e=rx1; } if(f<ry1) { e+=(ry1-f)/m; f=ry1; } else if(f>ry2)
  • 4. 4 Clipping/fill algorithms { e+=(ry2-f)/m; f=ry2; } x1[j]=e; y1[j]=f; j++; } } OUTPUT Enter the x1 & y1 coordinates: X1 : 70 Y1 : 80 Enter the x2 & y2 coordinates: X2 : 250 Y2 : 280
  • 5. 5 Clipping/fill algorithms //polygon clipping #include<graphics.h> #include<dos.h> #include<conio.h> #include<stdlib.h> void main() { int gd, gm ; int x1 , y1 , x2 , y2 ; int wxmin,wymin,wxmax, wymax ; float u1 = 0.0,u2 = 1.0 ; int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ; float r1 , r2 , r3 , r4 ; int x11 , y11 , x22 , y22 ; clrscr(); printf("Enter the windows left xmin , top boundry yminn"); scanf("%d%d",&wxmin,&wymin); printf("Enter the windows right xmax ,bottom boundry ymaxn"); scanf("%d%d",&wxmax,&wymax); printf("Enter line x1 , y1 co-ordinaten"); scanf("%d%d",&x1,&y1); printf("Enter line x2 , y2 co-ordinaten"); scanf("%d%d",&x2,&y2); printf("liang barsky express these 4 inequalities using lpk<=qpkn"); p1 = -(x2 - x1 ); q1 = x1 - wxmin ; p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ; p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
  • 6. 6 Clipping/fill algorithms p4 = ( y2 - y1 ) ; q4 = wymax - y1 ; printf("p1=0 line is parallel to left clippingn"); printf("p2=0 line is parallel to right clippingn"); printf("p3=0 line is parallel to bottom clippingn"); printf("p4=0 line is parallel to top clippingn"); if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) || ( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) || ( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) || ( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) ) { printf("Line is rejectedn"); getch(); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:tcbgi"); setcolor(RED); rectangle(wxmin,wymax,wxmax,wymin); setcolor(BLUE); line(x1,y1,x2,y2); getch(); setcolor(WHITE); line(x1,y1,x2,y2); getch(); } else { if( p1 != 0.0 ) { r1 =(float) q1 /p1 ; if( p1 < 0 ) u1 = max(r1 , u1 );
  • 7. 7 Clipping/fill algorithms else u2 = min(r1 , u2 ); } if( p2 != 0.0 ) { r2 = (float ) q2 /p2 ; if( p2 < 0 ) u1 = max(r2 , u1 ); else u2 = min(r2 , u2 ); } if( p3 != 0.0 ) { r3 = (float )q3 /p3 ; if( p3 < 0 ) u1 = max(r3 , u1 ); else u2 = min(r3 , u2 ); } if( p4 != 0.0 ) { r4 = (float )q4 /p4 ; if( p4 < 0 ) u1 = max(r4 , u1 ); else u2 = min(r4 , u2 ); } if( u1 > u2 ) printf("line rejectedn"); else
  • 8. 8 Clipping/fill algorithms { x11 = x1 + u1 * ( x2 - x1 ) ; y11 = y1 + u1 * ( y2 - y1 ) ; x22 = x1 + u2 * ( x2 - x1 ); y22 = y1 + u2 * ( y2 - y1 ); printf("Original line cordinatesn"); printf("x1 = %d , y1 = %d, x2 = %d, y2 = %dn",x1,y1,x2,y2); printf("Windows coordinate are n"); printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax); printf("New coordinates are n"); printf("x1 = %d, y1 = %d,x2 = %d , y2 = %dn",x11,y11,x22,y22); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:TCBGI"); setcolor(2); rectangle(wxmin,wymax,wxmax,wymin); setcolor(1); line(x1,y1,x2,y2); getch(); setcolor(0); line(x1,y1,x2,y2); setcolor(3); line(x11,y11,x22,y22); getch(); } } }
  • 9. 9 Clipping/fill algorithms //C Program For Oblique projection of a 3D object #include<stdio.h> #include<math.h> #include<graphics.h> main() { int x1,y1,x2,y2,gd,gm; int ymax,a[4][8]; float par[4][4],b[4][8]; int i,j,k,m,n,p; double L1,phi; a[0][0] = 100; a[1][0] = 100; a[2][0] = 100; a[0][1] = 200; a[1][1] = 100; a[2][1] = 100; a[0][2] = 200; a[1][2] = 200; a[2][2] = 100; a[0][3] = 100; a[1][3] = 200; a[2][3] = 100; a[0][4] = 100; a[1][4] = 100; a[2][4] = 200; a[0][5] = 200; a[1][5] = 100; a[2][5] = 200; a[0][6] = 200; a[1][6] = 200; a[2][6] = 200; a[0][7] = 100; a[1][7] = 200; a[2][7] = 200; phi = (double) (3.14*45.0)/180 ; L1 = 0.5; par[0][0] = 1; par[0][1] = 0; par[0][2] = L1*cos(phi); par[0][3] = 0;
  • 10. 10 Clipping/fill algorithms par[1][0] = 0; par[1][1] = 1; par[1][2] = L1*sin(phi); par[1][3] = 0; par[2][0] = 0; par[2][1] = 0; par[2][2] = 0; par[2][3] = 0; par[3][0] = 0; par[3][1] = 0; par[3][2] = 0; par[3][3] = 1; m=4; n=4; p=8; for(i=0; i<m; i++) for(k=0; k<p; k++) b[i][k] = 0; for(i=0; i<m; i++) for(k=0; k<p; k++) for(j=0; j<n; j++) b[i][k] += (float)par[i][j] * a[j][k]; detectgraph(&gd,&gm); initgraph(&gd,&gm, "c:tcbgi"); ymax = getmaxy(); /*- front plane display -*/ for(j=0;j<3;j++) { x1=(int) b[0][j]; y1=(int) b[1][j]; x2=(int) b[0][j+1]; y2=(int) b[1][j+1]; line( x1,ymax-y1,x2,ymax-y2);
  • 11. 11 Clipping/fill algorithms } x1=(int) b[0][3]; y1=(int) b[1][3]; x2=(int) b[0][0]; y2=(int) b[1][0]; line( x1,ymax-y1,x2,ymax-y2); /*- back plane display -*/ setcolor(11); for(j=4;j<7;j++) { x1=(int) b[0][j]; y1=(int) b[1][j]; x2=(int) b[0][j+1]; y2=(int) b[1][j+1]; line( x1,ymax-y1,x2,ymax-y2); } x1=(int) b[0][7]; y1=(int) b[1][7]; x2=(int) b[0][4]; y2=(int) b[1][4]; line( x1,ymax-y1,x2,ymax-y2); setcolor(13); for(i=0;i<4;i++) { x1=(int) b[0][i]; y1=(int) b[1][i]; x2=(int) b[0][4+i]; y2=(int) b[1][4+i]; line( x1,ymax-y1,x2,ymax-y2); } getch(); getch(); }
  • 12. 12 Clipping/fill algorithms C Program to implement Boundary Fill Algorithm #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void fill_right(x,y) int x , y ; { if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED)) { putpixel(x,y,RED); fill_right(++x,y); x = x - 1 ; fill_right(x,y-1); fill_right(x,y+1); } delay(1); } void fill_left(x,y) int x , y ; { if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED)) { putpixel(x,y,RED); fill_left(--x,y); x = x + 1 ; fill_left(x,y-1);
  • 13. 13 Clipping/fill algorithms fill_left(x,y+1); } delay(1); } void main() { int x,y,n,i; int gd=DETECT,gm; clrscr(); initgraph(&gd,&gm,"c:tcbgi"); /*- draw object -*/ line (50,50,200,50); line (200,50,200,300); line (200,300,50,300); line (50,300,50,50); /*- set seed point -*/ x = 100; y = 100; fill_right(x,y); fill_left(x-1,y); getch(); }
  • 14. 14 Clipping/fill algorithms //C Program to Implement Flood Fill Algorithm #include<conio.h> #include<stdio.h> #include<graphics.h> #include<dos.h> void fill_right(x,y) int x , y ; { if(getpixel(x,y) == 0) { putpixel(x,y,RED); fill_right(++x,y); x = x - 1 ; fill_right(x,y-1); fill_right(x,y+1); } } void fill_left(x,y) int x , y ; { if(getpixel(x,y) == 0) { putpixel(x,y,RED); fill_left(--x,y); x = x + 1 ; fill_left(x,y-1); fill_left(x,y+1);
  • 15. 15 Clipping/fill algorithms } } void main() { int x , y ,a[10][10]; int gd, gm ,n,i; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:tcbgi"); printf("nntEnter the no. of edges of polygon : "); scanf("%d",&n); printf("nntEnter the cordinates of polygon :nnn "); for(i=0;i<n;i++) { printf("tX%d Y%d : ",i,i); scanf("%d %d",&a[i][0],&a[i][1]); } a[n][0]=a[0][0]; a[n][1]=a[0][1]; printf("nntEnter the seed pt. : "); scanf("%d%d",&x,&y); cleardevice(); setcolor(WHITE); for(i=0;i<n;i++) /*- draw poly -*/ {
  • 16. 16 Clipping/fill algorithms line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } fill_right(x,y); fill_left(x-1,y); getch(); } /*SAMPLE INPUT*/ /*Enter the number of edges of polygon 4 X0 Y0 = 50 50 X1 Y1 = 200 50 X2 Y2 = 200 300 X3 Y3 = 50 300 Enter the seed point 100 100*/