Ex.
no :1
IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE DRAWING
Date:30-07-2019
AIM:
To write a program to implement Midpoint algorithms for line, circle and ellipse drawing.
ALGORITHM:
Step1: Create a function for line,circle and ellipse.
Step2: Take two input coordinates(x1,y1) and (x2,y2).Calculate dx,dy and d=dy-dx/2,then draw the line.
Step3: Input radius r and center(x,y).Calculate the initial value parameter as p0=5/4-r and draw the circle using
putpixel(x+a,y+b).
Step4: Calculate dx=2*yrad*yrad*x,dy=2*xrad*xrad*y,p1=(yrad*yrad)+((xrad*xrad)/4)-yrad*(xrad*xrad) to
draw the ellipse using putpixel(x+a,y+b,RED).
Step5: Display line, circle and ellipse drawing of Midpoint algorithm.
1
PROGRAM:
Index.c
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include "eli.h"
#include "cir.h"
#include "lin.h"
void main()
{
int choice=0;
char z;
clrscr();
z='Y';
while (z!='N'){
system("cls");
printf("\n1.ellipse\n2.circle\n3.Line\n4.exit\n\nEnter your Choice [1-4]? : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
eli();
break;
case 2:
cir();
break;
case 3: lin();
break;
case 4:
exit(0);
default:
printf("Enter valid option\n");
}
printf("\nDo you want to continue? (Y/N):");
2
while(getchar() != '\n') continue;
scanf("%c",&z);
if(isalpha(z))
z=toupper(z);
// else
// printf("\nYou should enter a character to continue\n");
}
system("pause");
getch();
Eli()
void eli()
{
float x,y,a,b,h,k,p1,p2,dx,dy;
int gd= DETECT,gm;
clrscr();
initgraph (&gd, &gm,"C:\\TURBOC3\\BGI");
printf("\n ENTER CENTER COORDINATES (h,k) OF ELLIPSE:\n");
scanf("%f%f",&h,&k) ;
printf("\n ENTER LENGTH OF MAJOR AXIS\n:");
scanf("%f",&a);
printf("\n ENTER LENGTH OF MINOR AXIS:\n");
scanf("%f",&b);
x=0;
y=b;
p1 =(b * b)+((a*a)/4)-(b*a*a);
dx=2*(b*b)*x;
dy=2*(a*a)*y;
while(dx<dy)
{
putpixel (x+h, y+k, RED);
3
putpixel (-x+h, -y+k, RED);
putpixel (x+h, -y+k, RED);
putpixel (-x+h, y+k, RED);
if (p1 < 0)
{
x=x+1;
dx=2*b*b*x;
p1=p1+dx+(b*b);
}
else
{
x=x+1;
y=y-1;
dx=2*b*b*x;
dy=2*a*a*y;
p1=p1+(dx-dy)+(b*b);
}
p2 =((b * b)* (x + 0.5)*(x + 0.5))+((a * a)*(y-1) * (y-1))-(a * a *b * b);
while (y>=0)
{
putpixel (x+h, y+k, RED);
putpixel (-x+h, -y+k, RED);
putpixel (x+h, -y+k, RED);
putpixel (-x+h, y+k, RED);
if (p2>0)
{
y=y-1;
dy=2*(a*a)*y;
p2=p2-dy+(a *a); }
else
{
x=x+1;
y=y-1;
dx=dx+2*(b*b);
dy=dy-2*(a*a);
p2=p2+(dx-dy)+(a*a)
}
} }
4
Cir()
void cir()
{
int r,x,y,gd=DETECT,gm,h,k;
float p;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n ENTER CENTER COORDINATE OF CIRCLE:\n");
scanf("%d%d",&h,&k) ;
printf("Enter radius:\n ");
scanf("%d",&r);
x=0;
y=r;
p=1.25-r;
while(x<y)
{
putpixel(h+x,k+y,RED);
putpixel(h+x,k-y,WHITE);
putpixel(h-x,k+y,BLUE);
putpixel(h-x,k-y,GREEN);
putpixel(h+y,k+x,YELLOW);
putpixel(h+y,k-x,RED);
putpixel(h-y,k+x,WHITE);
putpixel(h-y,k-x,BLUE);
if (p<0)
{
x=x+1;
y=y;
p=p+2*x+3;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+5;}}}
5
lin()
void lin(){
int gd=DETECT, gm,x1,x2,y1,y2,dx,dy,d,cre,crne,x,y;
clrscr();
initgraph(&gd, &gm,"C:\\TURBOC3\\BGI");
printf("Enter (x1,y1):\n");
scanf("%d%d",&x1,&y1);
printf("Enter (x2,y2):\n");
scanf("%d%d",&x2,&y2);
dx = x2 - x1;
dy = y2 - y1;
d = 2 * dy - dx;
cre = 2 * dy;
crne = 2 * (dy - dx);
x = x1;
y = y1;
putpixel(x, y, WHITE);
while(x < x2){
if(d<=0){
d += cre;
x++;
}
else{
d += crne;
x++;
y++;
}
putpixel(x,y,WHITE);
}
}
6
OUTPUT:-
Enter the Choice:1
Enter the Choice:2
7
Enter the Choice:3
RESULT:
Thus the program implementation of Midpoint algorithm for line, circle and ellipse drawing is done
successfully
8
Ex.no :2
IMPLEMENTATION OF POLYGON CLIPPING AND POLYGON
Date:13-08-2019 FILLING
AIM:
To create a program to implement polygon clipping and polygon filling Using flood fill algorithm.
ALGORITHM:
Step 1: Start the Program.
Step 2: Include the graphics header file
Step 3: Create a function to get the line co-ordinates and window co-ordinate values.
Step 4: Fill the polygon using flood fill algorithm,flood fill(x,y,new color,old color)
Step5: Check all the co-ordinates of the lines are within the window co-ordinate or not.
Step6: Clip the lines which are all outside of the window co-ordinates using Sutherland- Holygon polygon
clipping algortthm.
Step7: After the clipping draw all the lines along with the window.
9
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.*;
public class Application extends JPanel implements MouseListener {
private static final int WIDTH = 720;
private static final int HEIGHT = 480;
private static final int BOUNDARY_RGB=Color.blue.getRGB();
private static final Color BLUE=Color.BLUE;
private BufferedImage mImage;
private ArrayList<Point> mVertices;
private Integer mFloodPoint;
private static Point point;
private void clearImage(){
mVertices = new ArrayList<>();
Graphics2D graphics2D = mImage.createGraphics();
graphics2D.setBackground(Color.black);
graphics2D.clearRect(0,0,WIDTH,HEIGHT);
repaint();
revalidate();
10
}
Application(){
mImage = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_3BYTE_BGR);
mVertices = new ArrayList<>();
mFloodPoint = 0;
point = new Point(0,0);
addMouseListener(this);
}@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics2D = mImage.createGraphics();
graphics2D.setBackground(Color.black);
g.drawImage(mImage,0,0,this);
}@Override
public void mouseClicked(MouseEvent e) {}
private boolean isVertexPoint(int x, int y){
for(Point point :mVertices)
if(point.X == x && point.Y == y)
return true;
return false;
private boolean isInside(int x, int y) {
boolean result = false;
int i,j;
11
for (i = 0, j = mVertices.size()- 1; i < mVertices.size(); j = i++) {
if ((mVertices.get(i).Y > y) != (mVertices.get(j).Y > y) &&
(x < (mVertices.get(j).X - mVertices.get(i).X) *(y - mVertices.get(i).Y)
(mVertices.get(j).Y-mVertices.get(i).Y) + mVertices.get(i).X))
result = !result;
return result;
private void drawPolygon() {
if(mVertices.size() >= 3){
int totalVertices = mVertices.size();
Graphics2D graphics2D = mImage.createGraphics();
graphics2D.setColor(BLUE);
for(int i = 0 ; i < totalVertices - 1; ++i){
Point current = mVertices.get(i);
Point next = mVertices.get(i+1);
graphics2D.drawLine(current.X,current.Y,next.X, next.Y);
Point first = mVertices.get(0);
Point last = mVertices.get(totalVertices -1);
graphics2D.drawLine(first.X,first.Y,last.X,last.Y);
12
repaint();
revalidate();
} @Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
System.out.println("Adding point : " + e.getX()+" "+e.getY());
mVertices.add(new Point(e.getX(),e.getY()));
else if(e.getButton() == MouseEvent.BUTTON3){
if(mFloodPoint==0) {
System.out.println("Drawing polygon");
drawPolygon();
mFloodPoint = 1;
else if(mFloodPoint==1){
System.out.println("Filling polygon");
floodFill(e.getX(),e.getY(),Color.green);
mFloodPoint = 2;
else if(mFloodPoint==2){
Scanner in = new Scanner(System.in);
System.out.println("Enter first corner point of clip window, width height x y h w: ");
point.X = in.nextInt(); point.Y = in.nextInt();
13
int h = in.nextInt();
int w = in.nextInt();
in.close();
System.out.println("Clipping polygon");
clip(w, h);
mFloodPoint = 2;
System.out.println("Clipping polygon finished");
else if(e.getButton() == MouseEvent.BUTTON2){
clearImage();
private void floodFill(int x, int y, Color fillColor) {
Stack<Point> callStack = new Stack<>();
callStack.add(new Point(x,y));
while(!callStack.isEmpty()) {
Point point = callStack.pop();
if(isInside(point.X, point.Y)) {
if(mImage.getRGB(point.X, point.Y) != fillColor.getRGB() /*&&
mImage.getRGB(point.X, point.Y) != BOUNDARY_RGB*/) {
//System.out.println("adding point " + point.toString());
mImage.setRGB(point.X, point.Y, fillColor.getRGB());
14
repaint();
revalidate();
callStack.add(new Point(point.X + 1, point.Y));
callStack.add(new Point(point.X - 1, point.Y));
callStack.add(new Point(point.X, point.Y + 1));
callStack.add(new Point(point.X, point.Y - 1));
repaint();
private void clip(int w, int h){
for(int i=0;i<mImage.getWidth(); i++){
for(int j=0;j<mImage.getHeight(); j++){
if((i<=point.X || i>=point.X + w) || (j<=point.Y || j>=point.Y + h)){
mImage.setRGB(i, j, Color.black.getRGB());
repaint();
}@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
15
@Override
public void mouseExited(MouseEvent e) {}
class Point{
int X, Y;
Point(int X, int Y){
this.X = X;
this.Y = Y;
public static void main(String[] args) {
Application app = new Application();
JFrame jFrame = new JFrame();
jFrame.setSize(720, 480);
jFrame.setBackground(Color.BLUE);
jFrame.add(app);
jFrame.setVisible(true);
16
OUTPUT:-
17
RESULT:
Thus the program implementation of Sutherland Hodgeman algorithm for polygon fill and clip the polygon is
done successfully.
18
Ex.no:3
IMPLEMENTATION OF 2D AND 3D TRANSFORMATION
Date:03-09-2-19
AIM:
To write a program to implement of 2d and 3d transformation using Blender Tool.
ALGORITHM:
Step1: Install the Blender application.
Step2: click file -> New
Step3: Draw a model for 2d transformation and Rectangle,Cone and cubic surface for 3d and Develop a
chair model for 3d transformation.
Step4: Open video stream and apply scale,rotation and drag transformations with the specified time locations
Step5: Set fixed value for scaling,rotation,drag and X,Y co-ordinates and set Z=0 for 2d transformation.
Step6: Set fixed value for scaling,rotation,drag and X,Y,Z co-ordinates for 3d transformation.
19
OUTPUT:-
2D
3D
RESULT:
Thus the program implementation of 2D and 3D graphic transformation is done successfully.
20
Ex.no :4 ANIMATION CREATION – 1
Date:17-09-2019
AIM:
To create a program to implement the graphic animation using C programming language.
.
ALGORITHM:
Step 1: Include the necessary library functions for graphic c program.
Step 2: Declare the necessary variables.
Step 3: Create the main function, in that function to initialize graph.
Step 4: Using line, rectangle function for creating house.
Step 5: Using line, rectangle function for creating vehicles.
Step 6: Similarly, using line, circle function for creating man
Step 7: For making a man to walk and vehicle to move use delay function.
Step 7: Stop the program.
21
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#define ScreenWidth getmaxx()
#define ScreenHeight getmaxy()
#define midy getmaxy()/2
#define GroundY ScreenHeight*0.75
int ldisp=0;
void GraphicProcess(int x,int ldisp,int i,int j,int k)
{
setcolor(WHITE);
rectangle(150,180,250,midy+35);
rectangle(250,180,420,midy+35);
rectangle(180,250,220,midy+35);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
rectangle(50+k,275,150+k,400);
rectangle(150+k,350,200+k,400);
circle(75+k,410,10);
circle(175+k,410,10);
setcolor(j);
setcolor(WHITE);
line(0, midy + 37, ScreenWidth, midy + 37);
setcolor(YELLOW);
setfillstyle(SOLID_FILL, RED);
line(i, midy + 23, i, midy);
line(i, midy, 40 + i, midy - 20);
line(40 + i, midy - 20, 80 + i, midy - 20);
line(80 + i, midy - 20, 100 + i, midy);
line(100 + i, midy, 120 + i, midy);
line(120 + i, midy, 120 + i, midy + 23);
line(0 + i, midy + 23, 18 + i, midy + 23);
arc(30 + i, midy + 23, 0, 180, 12);
line(42 + i, midy + 23, 78 + i, midy + 23);
arc(90 + i, midy + 23, 0, 180, 12);
line(102 + i, midy + 23, 120 + i, midy + 23);
line(28 + i, midy, 43 + i, midy - 15);
line(43 + i, midy - 15, 57 + i, midy - 15);
line(57 + i, midy - 15, 57 + i, midy);
line(57 + i, midy, 28 + i, midy);
22
line(62 + i, midy - 15, 77 + i, midy - 15);
line(77 + i, midy - 15, 92 + i, midy);
line(92 + i, midy, 62 + i, midy);
line(62 + i, midy, 62 + i, midy - 15);
floodfill(5 + i, midy + 22, YELLOW);
setcolor(BLUE);
setfillstyle(SOLID_FILL, DARKGRAY);
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
floodfill(30 + i, midy + 25, BLUE);
floodfill(90 + i, midy + 25, BLUE);
setcolor(WHITE);
circle(x,GroundY-90,10);
line(x,GroundY-80,x,GroundY-30);
line(x,GroundY-70,x+15+ldisp,GroundY-60);
line(x,GroundY-65,x+ldisp,GroundY-55);
line(x,GroundY-30,x+ldisp,GroundY);
line(x,GroundY-30,x-ldisp,GroundY);
}
void main()
{
int gd=DETECT,gm,x=0,i=0,j=0,k=0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
while(!kbhit())
{
ldisp=(ldisp+2)%20;
GraphicProcess(x,ldisp,i,j,k);
delay(50);
cleardevice();
x=(x+2)%ScreenWidth;
j++;
k=k+10;
i=i+5;
}
getch();
}
23
OUTPUT:
RESULT:
Thus the program implementation of person walking in the city road is done successfully.
24
Ex.no :5
ANIMATION CREATION - 2
Date:15-10-2019
AIM:
To create a man cycling animation program using JAVA.
ALGORITHM:
Step 1: Include the necessary library functions for graphics java program.
Step 2: Declare the necessary variables.
Step 3: Read the gif files.
Step 4: Resize the background image and gif as per the screen size.
Step 5: Insert moving object in a loop to repeat the process.
Step 6: Stop the program
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
public class Park extends JFrame {
private int frameWidth = 700, frameHeight = 500;
private Image image = new ImageIcon("F:/abc.jpg").getImage();
private Image image1 = new ImageIcon("C:/Users/91807/Desktop/cd.gif").getImage();
public Park() {
setBounds(100, 100, frameWidth, frameHeight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
g.drawImage(image, 0,0,getWidth(),getHeight(),this);
for(int i=0;i<=getWidth();i++)
25
{
g.drawImage(image, 0,0,getWidth(),getHeight(),this);
g.drawImage(image1, i,getHeight()-100,100,100,this);
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}
public static void main(String args[]) {
new Park();
}
}
26
OUTPUT:
RESULT:
Thus the program implementation of a man cycling in the park is done successfully.
27
Ex.no :6
ANIMATION CREATION - 3
Date:30-10-2019
AIM
To create an animation using graphic tool like blender.
PROCEDURE:
Building a Warrior
Step1: For creating a Warrior animation using blender.
Step2: Open the blender software for deleting the default cube by Alt + X.
Step3: for adding press shift + A to add UV sphere.
Step4: for recompress the UV sphere.
Step5: Add duplicate of the UV sphere to place the body for the Warrior.
Step6: Repeat the step for creating face.
Step7: To move the mirror to place an eye.
Step8: Repeat to create another eye.
Step9: Press Shift + A to add cone to create a nose like structure.
Step10: Press S to resize the cone.
Step11: Add cylinder to create leg structure
Step12: Repeat the step to create a warrior and make duplicate by press Shift + D.
Rendering into MOVE file
Step13: for moving an Animated image.
Step14: In camera use the frame size for rendering image.
Step15: in output window select the source folder to save the image may be in png/jpg format.
Step16: After save the image.
Step17: view-> render animation to play a animation.
Step18: after saving a file view -> view animation.
28
OUTPUT:
RESULT:
Thus the program to animate and simulate a cartoon character is done successfully
29