You are on page 1of 32

CONTENT

EX NO DATE TITLE PAGE SIGNATURE

IMPLEMENTATION OF LINE, CIRCLE AND


1 30-07-2019 ELLIPSE DRAWING 4-10

2 20-08-2019 IMPLEMENTATION OF POLYGON CLIPPING 11-20


AND POLYGON FILLING

3 03-09-2019 IMPLEMENTATION OF 2D AND 3D 21-22


TRANSFORMATION

4 17-09-2019 ANIMATION CREATION – 1 23-26

5 22-10-2019 ANIMATION CREATION – 2 27-29

6 30-10-2019 ANIMATION CREATION – 3 30-31


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:
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<process.h>
void drawline(int x1,int y1,int x2,int y2)
{
int x,y;
float dx,dy,d;
dy = y2- y1;
dx = x2 - x1;
d = dy - (dx/2);
x =x1;
y = y1;
putpixel(x , y,RED);
while(x < x2)
{
x = x+1;
if (d < 0)
d = d + dy;
else{
d = d + dy - dx;
y = y+1;
}
putpixel(x,y,RED);
} getch();
}
void drawcircle(int a,int b,int r)
{
int x=0;
int y=r;
int p=1.25-r;
while(x<=y){
putpixel(x+a,y+b,RED);
putpixel(x+a,-y+b,RED);
putpixel(-x+a,-y+b,RED);
putpixel(y+a,x+b,RED);
putpixel(-y+a,-x+b,RED);
putpixel(-x+a,y+b,RED);
2
putpixel(-y+a,x+b,RED);
if(p<0){
x+=1;
p+=2*x+3;
}
Else{
x+=1;
y-=1;
p+=2*(x-y)+5;
}
} getch();
}
void drawellipse(int a,int b,float xrad,float yrad)
{
float x,y,p1,p2,dx,dy;
x=0;
y=yrad;
dx=2*yrad*yrad*x;
dy=2*xrad*xrad*y;
p1=(yrad*yrad)+((xrad*xrad)/4)-yrad*(xrad*xrad);
while(dx<dy){
putpixel(x+a,y+b,RED);
putpixel(x+a,-y+b,RED);
putpixel(-x+a,-y+b,RED);
putpixel(-x+a,y+b,RED);
if(p1<0){
x+=1;
dx=2*yrad*yrad*x;
p1+=2*yrad*yrad*x+yrad*yrad;
}
else{
x+=1;
y-=1;
dx=2*yrad*yrad*x;
dy=2*xrad*xrad*y;
p1+=dx-dy+yrad*yrad;

}
}

3
p2=yrad*yrad*(x+0.5)*(x+0.5)+(xrad*xrad*(y-1)*(y-1))-xrad*xrad*yrad*yrad;
while(y>=0){
putpixel(x+a,y+b,RED);
putpixel(x+a,-y+b,RED);
putpixel(-x+a,-y+b,RED);
putpixel(-x+a,y+b,RED);
if(p2>0){
y-=1;
dy=2*xrad*xrad*y;
p2-=dy+xrad*xrad;
}
else{
x+=1;
y-=1;
dx+=2*yrad*yrad;
dy-=2*xrad*xrad;
p2+=dx-dy+xrad*xrad;
}
} getch();

}
void main()
{ int ch,x,y,r,x1,x2,y1,y2,a,b;
float dx,dy,xrad=100,yrad=50,d;
int gd =DETECT ,gm;
char chdraw;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
while(1){
clearviewport();
cout<<"1.draw line\n2.draw circle\n3.draw ellipse\n4.exit\n";
cin>>ch;
switch(ch){
Case 1: clrscr();
cout<<"Enter the coordinates of first point:";
cin>>x1>>y1;
cout<<"Enter the coordinates of second point:";
cin>>x2>>y2;
drawline(x1,y1,x2,y2);
break;
4
Case 2: clrscr();
cout<<"Enter the coordinates of center of circle:";
cin>>a>>b;
cout<<"Enter the radius of circle:";
cin>>r;
drawcircle(a,b,r);
break;
Case 3: clrscr();
cout<<"Enter the coordinates of center of ellipse:";
cin>>a>>b;
cout<<"Enter the major axis and minor axis values:";
cin>>xrad>>yrad;
drawellipse(a,b,xrad,yrad);
break;
Case 4: exit(0);
break;
Default: cout<<"\n Invalid input";

}
}
}

5
OUTPUT:-

1.Line

2. Circle

3. Ellipse
4. Exit

Enter the Choice:1

Enter the Choice:2

6
Enter the Choice:3

RESULT:

Thus the program implementation of Midpoint algorithm for line, circle and ellipse drawing is done
successfully.

7
AIM:
To create a program to implement polygon clipping and polygon filling Using flood fill algorithm.

Ex.no :2
IMPLEMENTATION OF POLYGON CLIPPING AND POLYGON FILLING
Date:13-08-2019

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)

Step 5: Check all the co-ordinates of the lines are within the window co-ordinate or

not.

Step 6: Clip the lines which are all outside of the window co-ordinates using Sutherland- Holygon polygon

clipping algortthm.

Step 7: After the clipping draw all the lines along with the window.
8

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();
9

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;
for (i = 0, j = mVertices.size()- 1; i < mVertices.size(); j = i++) {

10

(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);

repaint();

revalidate();
11

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();

int h = in.nextInt();

int w = in.nextInt();

in.close();

System.out.println("Clipping polygon");
clip(w, h);

12

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());

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));

}
}

13

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) {}

@Override

public void mouseExited(MouseEvent e) {}

class Point{

int X, Y;

Point(int X, int Y){

this.X = X;

this.Y = Y;

}
}

14

Application app = new Application();

JFrame jFrame = new JFrame();

jFrame.setSize(720, 480);

jFrame.setBackground(Color.BLUE);

jFrame.add(app);

jFrame.setVisible(true);

}
15

OUTPUT:-
16

RESULT:

Thus the program implementation of Sutherland Hodgeman algorithm for polygon fill and clip the
polygon is done successfully.
17
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: Insert models of a helicopter model for 2d transformation and Rectangle,Cone and cubic surface for 3d

and Develop a table 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.

18
OUTPUT:-

2D

3D

RESULT:
Thus the program implementation of 2D and 3D graphic transformation is done successfully.

19
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 Ellipse function for creating cloud.

Step 6: Similarly, using line, circle function for creating man

Step 7: For making a man to walking using delay function.

Step 7: Stop the program.


20

PROGRAM:

#include<stdio.h>
#include<graphics.h>

#define ScreenWidth getmaxx()


#define ScreenHeight getmaxy()
#define GroundY ScreenHeight*0.75

int ldisp=0;

void DrawManAndUmbrella(int x,int ldisp)


{
//head
circle(x,GroundY-90,10);
line(x,GroundY-80,x,GroundY-30);
//hand
line(x,GroundY-70,x+10,GroundY-60);
line(x,GroundY-65,x+10,GroundY-55);
line(x+10,GroundY-60,x+20,GroundY-70);
line(x+10,GroundY-55,x+20,GroundY-70);
//legs
line(x,GroundY-30,x+ldisp,GroundY);
line(x,GroundY-30,x-ldisp,GroundY);
//umbrella
pieslice(x+20,GroundY-120,0,180,40);
line(x+20,GroundY-120,x+20,GroundY-70);
}

void Rain(int x)
{
int i,rx,ry;
for(i=0;i<400;i++)
{
rx=rand() % ScreenWidth;
ry=rand() % ScreenHeight;
if(ry<GroundY-4)
{
//if(ry<GroundY-120 || (ry>GroundY-120 && (rx<x-20 || rx>x+60)))
line(rx,ry,rx+0.5,ry+4);
}
}
}
void main()
{
int gd=DETECT,gm,x=0;
//Change BGI directory according to yours
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

while(!kbhit())
{
//Draw Ground
line(0,GroundY,ScreenWidth,GroundY);
//Rain(x);
ldisp=(ldisp+2)%20;
DrawManAndUmbrella(x,ldisp);
delay(75);
cleardevice();
x=(x+2)%ScreenWidth;
}
getch();
}
22

OUTPUT:

RESULT:
Thus the program implementation of person walking in the city road is done successfully.
23

Ex.no :5
ANIMATION CREATION - 2
Date:15-10-2019

AIM:

To create a man cycling animation program using java.

PROCEDURE:
Choice of language : java

Step 1: Include the necessary library functions for graphic java program.

Step 2: Declare the necessary variables.

Step 3: Create the main function, in that function include the paint() method.

Step 4: Import the necessary images to the variable image.

Step 5: Set boundaries and resize the image frame.

Step 6: Draw the image using g.drawImage() method

Step 7: Use Thread.sleep() method for making the dalay.

Step 7: Make it in a loop for a certain boundary.


24
PROGRAM:

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
public class MoveImages extends JFrame {
private int frameWidth = 700, frameHeight = 500;
private Image image = new ImageIcon("/home/ubais/PycharmProjects/minipro3/Car
Images/park11.jpg").getImage();
private Image image1 = new ImageIcon("/home/ubais/Desktop/cycle.gif").getImage();

public MoveImages() {
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);
//image = resize(image1,500,500);
for(int i=0;i<=getWidth();i++)
{
g.drawImage(image, 0,0,getWidth(),getHeight(),this);
g.drawImage(image1, i,300,100,100,this);
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}

public static void main(String args[]) {


new MoveImages();
}
}

25

OUTPUT:

RESULT:

Thus the program implementation of a man cycling in the park is done successfully.
26

Ex.no :6
ANIMATION CREATION - 3
Date:30-10-2019

AIM

To create a animation using graphic tool like blender.

PROCEDURE:
Building a Adventurer

Step1 : Open the blender software for deleting the default cube by Alt + X.
Step 2: for adding press shift + A to add UV sphere.
Step 3: for recompress the UV sphere.
Step 4: Add duplicate of the UV sphere to place the body for the Adventure.
Step 5: Repeat the step for creating face.
Step 6: To move the mirror to place a eye.
Step 7: Repeat to create another eye.
Step 8: Press Shift + A to add cone to create a nose like structure.
Step 9: Press S to resize the cone.
Step 10: Add cylinder to create hat like structure
Step 11: Repeat the step to create a hat and make duplicate by press Shift + D.

Rendering into MOVE file

Step13: for moving a 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.
27

OUTPUT:

You might also like