LAB EXERCISE #1
Problem 1: WAP to draw a line using DDA algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{
float x,y,x1,y1,x2,y2,dx,dy,length;
int gm,gd=DETECT,i;
printf("enter the value of x1:\t");
scanf("%f",&x1);
printf("enter the value of y1:\t");
scanf("%f",&y1);
printf("enter the value of x2:\t");
scanf("%f",&x2);
printf("enter the value of y2:\t");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
length=dx;
}
else
{
length=dy;
}
dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<=length)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
Problem 2: WAP to draw a line using Bresenham’s algorithm.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{
clrscr();
float x1,y1,x2,y2,dx,dy,p;
int gm,gd=DETECT,i=1;
printf("Enter The Value of x1:");
scanf("%f",&x1);
printf("Enter The Value of y1: ");
scanf("%f",&y1);
printf("Enter The Value of x2: ");
scanf("%f",&x2);
printf("Enter The Value of y2: ");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
p=((2*dy)-dx);
while(i<=dx)
{
putpixel(x1,x2,15);
if(p<0)
{
++x1;
p=p+(2*dy);
}
else
{
++x1;
++y1;
p=p+(2*dy)-(2*dx);
}
i=i+1;
delay(100);
}
getch();
closegraph();
}
Problem 3: WAP to draw a circle using Bresenham’s algorithm.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{ float x,y,r,p;
int gm,gd=DETECT;
printf("Enter the radius: ");
scanf("%f",&r);
x=0;
y=r;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
p=1-r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+x,200-y,15);
putpixel(200+y,200-x,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-x,200+y,15);
putpixel(200-y,200+x,15);
if(p<0)
{
++x;
p=p+(2*x)+1;
}
else
{
++x;
--y;
p=p+(2*x)-(2*y)+1;
}
delay(100);
}while(x<y);
getch();
closegraph();
}
Problem 4: WAP to draw ellipse using Bresenham’s algorithm.
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
void main()
{ int gm,gd=DETECT;
int Rx,Ry,P10,x,y,a,b,P20;
printf("Enter Value of Rx and Ry: ");
scanf("%d %d",&Rx,&Ry);
x=0;
y=Ry;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
P10=(Ry*Ry)-(Rx*Rx)-(Rx*Rx*Ry)+(0.25*Rx*Rx);
do
{
putpixel(200+x,200+y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
putpixel(200-x,200-y,15);
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
if(P10<0)
{
x++;
P10=P10+a+(Ry*Ry);
}
else
{
x++;
y--;
P10=P10+a+(Ry*Ry)-b;
}
delay(100);
}while(a>b);
P20=((Ry*Ry)*((x+0.5)*(x+0.5)))+((Rx*Rx)*((y-1)*(y-1)))-((Rx*Rx)*(Ry*Ry));
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
do
{
putpixel(200+x,200+y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
putpixel(200-x,200-y,15);
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
if(P20>0)
{
y--;
P20=P20-b+(Rx*Rx);
}
else
{
x++;
y--;
P20=P20-b+a+(Rx*Rx);
}
delay(100);
}while(y>0);
getch();
closegraph();
}
Problem 5: WAP to design national flag using set of lines generated by DDA or
Bresenham line drawing algorithm.
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
void main()
{
// Initialize of gdriver with
// DETECT macros
initgraph(&gd, &gm, "C:\\turboc3\\bgi");
// Creating the base rectangle
line(250, 100, 250, 600);
line(250, 100, 250, 600);
// Fill the White Color
setfillstyle(SOLID_FILL, WHITE);
// Create and fill the top strip
rectangle(225, 600, 275, 610);
rectangle(200, 610, 300, 620);
floodfill(227, 608, 15);
floodfill(202, 618, 15);
// Fill the Light Red Color
setfillstyle(SOLID_FILL, LIGHTRED);
// Create and fill the ashoka
// chakra with Blue
rectangle(250, 100, 650, 280);
line(250, 160, 650, 160);
floodfill(252, 158, 15);
// Fill the Blue Color
setfillstyle(SOLID_FILL, BLUE);
// Create and fill the left
// part of the middle strip
// Create a Circle
circle(450, 190, 30);
floodfill(452, 188, 15);
// Fill the White Color
setfillstyle(SOLID_FILL, WHITE);
// Create and fill the right
// part of the middle strip
line(250, 160, 480, 160);
line(250, 220, 480, 220);
floodfill(252, 162, 15);
// Fill the White Color
setfillstyle(SOLID_FILL, WHITE);
// Create and fill the bottom
// strip
line(480, 160, 650, 160);
line(480, 220, 650, 220);
floodfill(482, 162, 15);
// Fill the Green Color
setfillstyle(SOLID_FILL, GREEN);
line(250, 220, 650, 220);
floodfill(252, 278, 15);
// Close the initialized gdriver
closegraph();
}
LAB EXERCISE #2
Problem 1: WAP to implement line clipping algorithms Cohen-Sutherland.
#include <iostream>
using namespace std;
// Defining region codes
const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000
// Defining x_max, y_max and x_min, y_min for
// clipping rectangle. Since diagonal points are
// enough to define a rectangle
const int x_max = 10;
const int y_max = 8;
const int x_min = 4;
const int y_min = 4;
// Function to compute region code for a point(x, y)
int computeCode(double x, double y)
{
// initialized as being inside
int code = INSIDE;
if (x < x_min) // to the left of rectangle
code |= LEFT;
else if (x > x_max) // to the right of rectangle
code |= RIGHT;
if (y < y_min) // below the rectangle
code |= BOTTOM;
else if (y > y_max) // above the rectangle
code |= TOP;
return code;
}
// Implementing Cohen-Sutherland algorithm
// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)
void cohenSutherlandClip(double x1, double y1,
double x2, double y2)
{
// Compute region codes for P1, P2
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
// Initialize line as outside the rectangular window
bool accept = false;
while (true) {
if ((code1 == 0) && (code2 == 0)) {
// If both endpoints lie within rectangle
accept = true;
break;
}
else if (code1 & code2) {
// If both endpoints are outside rectangle,
// in same region
break;
}
else {
// Some segment of line lies within the
// rectangle
int code_out;
double x, y;
// At least one endpoint is outside the
// rectangle, pick it.
if (code1 != 0)
code_out = code1;
else
code_out = code2;
// Find intersection point;
// using formulas y = y1 + slope * (x - x1),
// x = x1 + (1 / slope) * (y - y1)
if (code_out & TOP) {
// point is above the clip rectangle
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
}
else if (code_out & BOTTOM) {
// point is below the rectangle
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
}
else if (code_out & RIGHT) {
// point is to the right of rectangle
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
}
else if (code_out & LEFT) {
// point is to the left of rectangle
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}
// Now intersection point x, y is found
// We replace point outside rectangle
// by intersection point
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
cout << "Line accepted from " << x1 << ", "
<< y1 << " to " << x2 << ", " << y2 << endl;
// Here the user can add code to display the rectangle
// along with the accepted (portion of) lines
}
else
cout << "Line rejected" << endl;
}
// Driver code
int main()
{
// First Line segment
// P11 = (5, 5), P12 = (7, 7)
cohenSutherlandClip(5, 5, 7, 7);
// Second Line segment
// P21 = (7, 9), P22 = (11, 4)
cohenSutherlandClip(7, 9, 11, 4);
// Third Line segment
// P31 = (1, 5), P32 = (4, 1)
cohenSutherlandClip(1, 5, 4, 1);
return 0;
}
Problem 2: WAP to implement Cyrus-Beck algorithm.
#include <SFML/Graphics.hpp>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using namespace sf;
// Function to draw a line in SFML
void drawline(RenderWindow* window, pair<int, int> p0, pair<int, int> p1)
{
Vertex line[] = {
Vertex(Vector2f(p0.first, p0.second)),
Vertex(Vector2f(p1.first, p1.second))
};
window->draw(line, 2, Lines);
}
// Function to draw a polygon, given vertices
void drawPolygon(RenderWindow* window, pair<int, int> vertices[], int n)
{
for (int i = 0; i < n - 1; i++)
drawline(window, vertices[i], vertices[i + 1]);
drawline(window, vertices[0], vertices[n - 1]);
}
// Function to take dot product
int dot(pair<int, int> p0, pair<int, int> p1)
{
return p0.first * p1.first + p0.second * p1.second;
}
// Function to calculate the max from a vector of floats
float max(vector<float> t)
{
float maximum = INT_MIN;
for (int i = 0; i < t.size(); i++)
if (t[i] > maximum)
maximum = t[i];
return maximum;
}
// Function to calculate the min from a vector of floats
float min(vector<float> t)
{
float minimum = INT_MAX;
for (int i = 0; i < t.size(); i++)
if (t[i] < minimum)
minimum = t[i];
return minimum;
}
// Cyrus Beck function, returns a pair of values
// that are then displayed as a line
pair<int, int>* CyrusBeck(pair<int, int> vertices[],
pair<int, int> line[], int n)
{
// Temporary holder value that will be returned
pair<int, int>* newPair = new pair<int, int>[2];
// Normals initialized dynamically(can do it statically also, doesn't matter)
pair<int, int>* normal = new pair<int, int>[n];
// Calculating the normals
for (int i = 0; i < n; i++) {
normal[i].second = vertices[(i + 1) % n].first - vertices[i].first;
normal[i].first = vertices[i].second - vertices[(i + 1) % n].second;
}
// Calculating P1 - P0
pair<int, int> P1_P0
= make_pair(line[1].first - line[0].first,
line[1].second - line[0].second);
// Initializing all values of P0 - PEi
pair<int, int>* P0_PEi = new pair<int, int>[n];
// Calculating the values of P0 - PEi for all edges
for (int i = 0; i < n; i++) {
// Calculating PEi - P0, so that the
// denominator won't have to multiply by -1
P0_PEi[i].first
= vertices[i].first - line[0].first;
// while calculating 't'
P0_PEi[i].second = vertices[i].second - line[0].second;
}
int *numerator = new int[n], *denominator = new int[n];
// Calculating the numerator and denominators
// using the dot function
for (int i = 0; i < n; i++) {
numerator[i] = dot(normal[i], P0_PEi[i]);
denominator[i] = dot(normal[i], P1_P0);
}
// Initializing the 't' values dynamically
float* t = new float[n];
// Making two vectors called 't entering'
// and 't leaving' to group the 't's
// according to their denominators
vector<float> tE, tL;
// Calculating 't' and grouping them accordingly
for (int i = 0; i < n; i++) {
t[i] = (float)(numerator[i]) / (float)(denominator[i]);
if (denominator[i] > 0)
tE.push_back(t[i]);
else
tL.push_back(t[i]);
}
// Initializing the final two values of 't'
float temp[2];
// Taking the max of all 'tE' and 0, so pushing 0
tE.push_back(0.f);
temp[0] = max(tE);
// Taking the min of all 'tL' and 1, so pushing 1
tL.push_back(1.f);
temp[1] = min(tL);
// Entering 't' value cannot be
// greater than exiting 't' value,
// hence, this is the case when the line
// is completely outside
if (temp[0] > temp[1]) {
newPair[0] = make_pair(-1, -1);
newPair[1] = make_pair(-1, -1);
return newPair;
}
// Calculating the coordinates in terms of x and y
newPair[0].first
t
= (float)line[0].first
+ (float)P1_P0.first * (float)temp[0];
newPair[0].second
= (float)line[0].second
+ (float)P1_P0.second * (float)temp[0];
newPair[1].first
= (float)line[0].first
+ (float)P1_P0.first * (float)temp[1];
newPair[1].second
= (float)line[0].second
+ (float)P1_P0.second * (float)temp[1];
cout << '(' << newPair[0].first << ", "
<< newPair[0].second << ") ("
<< newPair[1].first << ", "
<< newPair[1].second << ")";
return newPair;
}
// Driver code
int main()
{
// Setting up a window and loop
// and the vertices of the polygon and line
RenderWindow window(VideoMode(500, 500), "Cyrus Beck");
pair<int, int> vertices[]
= { make_pair(200, 50),
make_pair(250, 100),
make_pair(200, 150),
make_pair(100, 150),
make_pair(50, 100),
make_pair(100, 50) };
// Make sure that the vertices
// are put in a clockwise order
int n = sizeof(vertices) / sizeof(vertices[0]);
pair<int, int> line[] = { make_pair(10, 10), make_pair(450, 200) };
pair<int, int>* temp1 = CyrusBeck(vertices, line, n);
pair<int, int> temp2[2];
temp2[0] = line[0];
temp2[1] = line[1];
// To allow clipping and unclipping
// of the line by just pressing a key
bool trigger = false;
while (window.isOpen()) {
window.clear();
Event event;
if (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
if (event.type == Event::KeyPressed)
trigger = !trigger;
}
drawPolygon(&window, vertices, n);
// Using the trigger value to clip
// and unclip a line
if (trigger) {
line[0] = temp1[0];
line[1] = temp1[1];
}
else {
line[0] = temp2[0];
line[1] = temp2[1];
}
drawline(&window, line[0], line[1]);
window.display();
}
return 0;
}
Problem 3: WAP to implement Midpoint Subdivision algorithm.
/ C++ program for point clipping Algorithm
#include <bits/stdc++.h>
using namespace std;
// Function for point clipping
void pointClip(int XY[][2], int n, int Xmin, int Ymin,
int Xmax, int Ymax)
{
/*************** Code for graphics view
// initialize graphics mode
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
for (int i=0; i<n; i++)
{
if ( (XY[i][0] >= Xmin) && (XY[i][0] <= Xmax))
{
if ( (XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
putpixel(XY[i][0],XY[i][1],3);
}
}
**********************/
/**** Arithmetic view ****/
cout << "Point inside the viewing pane:" << endl;
for (int i = 0; i < n; i++)
{
if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax))
{
if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
cout <<"[" << XY[i][0] <<","<<XY[i][1]<<"] ";
}
}
// print point coordinate outside viewing pane
cout<<"\n"<< endl;
cout << "Point outside the viewing pane:"<<endl;
for (int i = 0; i < n; i++)
{
if ((XY[i][0] < Xmin) || (XY[i][0] > Xmax))
cout << "[" << XY[i][0] << "," << XY[i][1] << "] ";
if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax))
cout << "[" << XY[i][0] << "," << XY[i][1] << "] ";
}
}
// Driver code
int main()
{
int XY[6][2] = {{10, 10}, {-10, 10}, {400, 100},
{100, 400}, {400, 400}, {100, 40}};
// getmaxx() & getmaxy() will return Xmax, Ymax
// value if graphics.h is included
int Xmin = 0;
int Xmax = 350;
int Ymin = 0;
int Ymax = 350;
pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax);
return 0;
}
LAB EXERCISE #3
Problem 1: WAP to perform 2D translation.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point of line segment:");
scanf("%d %d",&x1,&y1);
printf("Enter the ending point of line segment:");
scanf("%d %d",&x2,&y2);
printf("Enter translation distances tx,ty:\n");
scanf("%d%d",&tx,&ty);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x4+2,y4+2,"Line after translation");
getch();
}
Problem 2: WAP to perform 2D scaling.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point coordinates:");
scanf("%f %f",&x1,&y1);
printf("Enter the ending point coordinates:");
scanf("%f %f",&x2,&y2);
printf("Enter scaling factors sx,sy:\n");
scanf("%f%f",&sx,&sy);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x3=x1*sx;
y3=y1*sy;
x4=x2*sx;
y4=y2*sy;
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after scaling");
getch();
}
Problem 3: WAP to perform 2D rotation.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,a,t;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter coordinates of starting point:\n");
scanf("%f%f",&x1,&y1);
printf("Enter coordinates of ending point\n");
scanf("%f%f",&x2,&y2);
printf("Enter angle for rotation\n");
scanf("%f",&a);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
t=a*(3.14/180);
x3=(x1*cos(t))-(y1*sin(t));
y3=(x1*sin(t))+(y1*cos(t));
x4=(x2*cos(t))-(y2*sin(t));
y4=(x2*sin(t))+(y2*cos(t));
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after rotation");
getch();
}
Problem 4: WAP to perform Shearing
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
float shx,shy;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter shear factor shy along y-axis :");
scanf("%f",­);
line(100,10,200,10);
line(200,10,200,200);
line(200,200,100,200);
line(100,200,100,10);
printf("Y-shear");
setcolor(12);
line(100,10+(shy*100),200,10+(shy*200));
line(200,10+(shy*200),200,200+(shy*200));
line(200,200+(shy*200),100,200+(shy*100));
line(100,200+(shy*100),100,10+(shy*100));
getch();
closegraph();
}
Problem 5: WAP to perform 3D rotation.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void rotate();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n3D Transformation Rotating\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw(); getch();
cleardevice();
rotate();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void rotate()
{
float t;
int a1,b1,a2,b2,dep;
printf("Enter the angle to rotate=");
scanf("%f",&t);
t=t*(3.14/180);
a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);
a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);
b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);
b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);
if(a2>a1)
dep=(a2-a1)/4;
else
dep=(a1-a2)/4;
bar3d(a1,b1,a2,b2,dep,1); setcolor(5);
}
Problem 6: WAP to perform 3D scaling.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void scale();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Scaling\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
scale();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void scale()
{
int x,y,a1,a2,b1,b2,dep;
printf("\n\n Enter scaling Factors:");
scanf("%d%d",&x,&y);
a1=mx+(x1-mx)*x;
a2=mx+(x2-mx)*x;
b1=my+(y1-my)*y;
b2=my+(y2-my)*y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
Problem 7: WAP to perform 3D translation.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void trans();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Translation\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
trans();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void trans()
{
int a1,a2,b1,b2,dep,x,y;
printf("\n Enter the Translation Distances:");
scanf("%d%d",&x,&y);
a1=x1+x;
a2=x2+x;
b1=y1+y;
b2=y2+y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
LAB EXERCISE #4
Program 1: WAP to draw 2D Bezier Curves.
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
void bezier (int x[4], int y[4])
{
int gd = DETECT, gm;
int i;
double t;
initgraph (&gd, &gm, "C:\\TurboC3\\BGI");
for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +
3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
for (i=0; i<4; i++)
putpixel (x[i], y[i], YELLOW);
getch();
closegraph();
return;
}
void main()
{
int x[4], y[4];
int i;
printf ("Enter the x- and y-coordinates of the four control points.\n");
for (i=0; i<4; i++)
scanf ("%d%d", &x[i], &y[i]);
bezier (x, y);
}
Program 2: WAP to draw 2D B-splines Curves
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<SDL2/SDL.h>
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
int mousePosX , mousePosY ;
int xnew , ynew ;
/*Function to draw all other 7 pixels present at symmetric position*/
void drawCircle(int xc, int yc, int x, int y)
{
SDL_RenderDrawPoint(renderer,xc+x,yc+y) ;
SDL_RenderDrawPoint(renderer,xc-x,yc+y);
SDL_RenderDrawPoint(renderer,xc+x,yc-y);
SDL_RenderDrawPoint(renderer,xc-x,yc-y);
SDL_RenderDrawPoint(renderer,xc+y,yc+x);
SDL_RenderDrawPoint(renderer,xc-y,yc+x);
SDL_RenderDrawPoint(renderer,xc+y,yc-x);
SDL_RenderDrawPoint(renderer,xc-y,yc-x);
}
/*Function for circle-generation using Bresenham's algorithm */
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
while (y >= x)
{
/*for each pixel we will draw all eight pixels */
drawCircle(xc, yc, x, y);
x++;
/*check for decision parameter and correspondingly update d, x, y*/
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
}
}
/* Function that take input as Control Point x_coordinates and
Control Point y_coordinates and draw bezier curve */
void bezierCurve(int x[] , int y[])
{
double xu = 0.0 , yu = 0.0 , u = 0.0 ;
int i = 0 ;
for(u = 0.0 ; u <= 1.0 ; u += 0.0001)
{
xu = pow(1-u,3)*x[0]+3*u*pow(1-u,2)*x[1]+3*pow(u,2)*(1-u)*x[2]
+pow(u,3)*x[3];
yu = pow(1-u,3)*y[0]+3*u*pow(1-u,2)*y[1]+3*pow(u,2)*(1-u)*y[2]
+pow(u,3)*y[3];
SDL_RenderDrawPoint(renderer , (int)xu , (int)yu) ;
}
}
int main(int argc, char* argv[])
{
/*initialize sdl*/
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
/*
This function is used to create a window and default renderer.
int SDL_CreateWindowAndRenderer(int width
,int height
,Uint32 window_flags
,SDL_Window** window
,SDL_Renderer** renderer)
return 0 on success and -1 on error
*/
if(SDL_CreateWindowAndRenderer(640, 480, 0, &window, &renderer) == 0)
{
SDL_bool done = SDL_FALSE;
int i = 0 ;
int x[4] , y[4] , flagDrawn = 0 ;
while (!done)
{
SDL_Event event;
/*set background color to black*/
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
/*set draw color to white*/
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
/* We are drawing cubic bezier curve
which has four control points */
if(i==4)
{
bezierCurve(x , y) ;
flagDrawn = 1 ;
}
/*grey color circle to encircle control Point P0*/
SDL_SetRenderDrawColor(renderer, 128, 128, 128, SDL_ALPHA_OPAQUE);
circleBres(x[0] , y[0] , 8) ;
/*Red Line between control Point P0 & P1*/
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer , x[0] , y[0] , x[1] , y[1]) ;
/*grey color circle to encircle control Point P1*/
SDL_SetRenderDrawColor(renderer, 128, 128, 128, SDL_ALPHA_OPAQUE);
circleBres(x[1] , y[1] , 8) ;
/*Red Line between control Point P1 & P2*/
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer , x[1] , y[1] , x[2] , y[2]) ;
/*grey color circle to encircle control Point P2*/
SDL_SetRenderDrawColor(renderer, 128, 128, 128, SDL_ALPHA_OPAQUE);
circleBres(x[2] , y[2] , 8) ;
/*Red Line between control Point P2 & P3*/
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer , x[2] , y[2] , x[3] , y[3]) ;
/*grey color circle to encircle control Point P3*/
SDL_SetRenderDrawColor(renderer, 128, 128, 128, SDL_ALPHA_OPAQUE);
circleBres(x[3] , y[3] , 8) ;
/*We are Polling SDL events*/
if (SDL_PollEvent(&event))
{
/* if window cross button clicked then quit from window */
if (event.type == SDL_QUIT)
{
done = SDL_TRUE;
}
/*Mouse Button is Down */
if(event.type == SDL_MOUSEBUTTONDOWN)
{
/*If left mouse button down then store
that point as control point*/
if(event.button.button == SDL_BUTTON_LEFT)
{
/*store only four points
because of cubic bezier curve*/
if(i < 4)
{
printf("Control Point(P%d):(%d,%d)\n"
,i,mousePosX,mousePosY) ;
/*Storing Mouse x and y positions
in our x and y coordinate array */
x[i] = mousePosX ;
y[i] = mousePosY ;
i++ ;
}
}
}
/*Mouse is in motion*/
if(event.type == SDL_MOUSEMOTION)
{
/*get x and y positions from motion of mouse*/
xnew = event.motion.x ;
ynew = event.motion.y ;
int j ;
/* change coordinates of control point
after bezier curve has been drawn */
if(flagDrawn == 1)
{
for(j = 0 ; j < i ; j++)
{
/*Check mouse position if in b/w circle then
change position of that control point to mouse new
position which are coming from mouse motion*/
if((float)sqrt(abs(xnew-x[j]) * abs(xnew-x[j])
+ abs(ynew-y[j]) * abs(ynew-y[j])) < 8.0)
{
/*change coordinate of jth control point*/
x[j] = xnew ;
y[j] = ynew ;
printf("Changed Control Point(P%d):(%d,%d)\n"
,j,xnew,ynew) ;
}
}
}
/*updating mouse positions to positions
coming from motion*/
mousePosX = xnew ;
mousePosY = ynew ;
}
}
/*show the window*/
SDL_RenderPresent(renderer);
}
}
/*Destroy the renderer and window*/
if (renderer)
{
SDL_DestroyRenderer(renderer);
}
if (window)
{
SDL_DestroyWindow(window);
}
}
/*clean up SDL*/
SDL_Quit();
return 0;
}
LAB EXERCISE #5
Program 1: WAP for filling a given rectangle object with color using flood fill
algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void floodFill(int x,int y,int ncolor,int ocolor)
{
if(getpixel(x,y)==ocolor)
{
putpixel(x,y,ncolor);
floodFill(x+1,y,ncolor,ocolor);
floodFill(x-1,y,ncolor,ocolor);
floodFill(x,y+1,ncolor,ocolor);
floodFill(x,y-1,ncolor,ocolor);
}
delay(1);
}
void main()
{
int x,y,ncolor=BLUE,ocolor=WHITE;
int midx,midy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://tc//bgi");
cleardevice();
printf("enter seed point:");
scanf("%d%d",&x,&y);
midx=getmaxx()/2;
midy=getmaxy()/2;
setbkcolor(RED);
setpalette(ocolor,GREEN);
fillellipse(midx,midy,50,25);
fillellipse(midx+100,midy+100,50,25);
floodFill(x,y,ncolor,ocolor);
getch();
closegraph();
}
Program 2: WAP for filling a given rectangle object with color
using boundary fill algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void boundaryFill(int x,int y)
{
int interiorColor;
interiorColor=getpixel(x,y);
if((interiorColor !=WHITE)&&(interiorColor !=RED))
{
putpixel(x,y,RED);
boundaryFill(x+1,y);
boundaryFill(x,y+1);
boundaryFill(x-1,y);
boundaryFill(x,y-1);
}
delay(1);
}
void main()
{
void boundaryFill(int,int);
int x,y,n,i;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
line(50,50,100,50);
line(100,50,100,100);
line(100,100,50,100);
line(50,100,50,50);
x=78,y=78;
boundaryFill(x,y);
getch();
closegraph();
}
Program 3: WAP to illustrate the use of setfill function.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
}