0% found this document useful (0 votes)
8 views5 pages

Cgam Lab 4 59

The document describes a lab exercise on 2D transformations in computer graphics. It includes code to: 1. Draw a rectangle and translate it 50 units in the x and y directions. 2. Draw a triangle, translate it 50 units in x and y, and color its boundaries. The code defines functions for drawing shapes, translating coordinates, and handling pens/colors. It demonstrates translating objects by modifying and redrawing their vertex coordinates.

Uploaded by

haziq shiekh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Cgam Lab 4 59

The document describes a lab exercise on 2D transformations in computer graphics. It includes code to: 1. Draw a rectangle and translate it 50 units in the x and y directions. 2. Draw a triangle, translate it 50 units in x and y, and color its boundaries. The code defines functions for drawing shapes, translating coordinates, and handling pens/colors. It demonstrates translating objects by modifying and redrawing their vertex coordinates.

Uploaded by

haziq shiekh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Computer Graphics Animation & Multimedia 2020-CE-059

LAB#4

Objective:
Implement 2D Transformation Translation Function with drawRect( ).

1. How to Draw a rectangle & translate it by a factor of 50.

Source Code:
TranslateView.h
// Operations
public:
int x[4], y[4];
int xN[4], yN[4];

// Implementation
public:
virtual ~CTranslateView();
void drawRect(int x[4 ], int y[4], CDC*);
void translate(int xN[4], int yN[4], int x[4],int y[4], int x1,int y1);

TranslateView.cpp
void CTranslateView::OnDraw(CDC* pDC)
{
CTranslateDoc* pDoc = GetDocument();
x[0]=200;
y[0]=100;
x[1]=200;
y[1]=30;
x[2]=270;
y[2]=30;
x[3]=270;
y[3]=100;
drawRect(x,y, pDC);
translate(xN, yN, x, y, 50,50);
drawRect(xN,yN, pDC);
pDC->MoveTo(x[0],y[0]);
pDC->LineTo(x[1], y[1]);
pDC->MoveTo(x[1],y[1]);
pDC->LineTo(x[2], y[2]);
pDC->MoveTo(x[2],y[2]);
pDC->LineTo(x[3], y[3]);
pDC->MoveTo(x[3],y[3]);
pDC->LineTo(x[0], y[0]);

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

}
void CTranslateView::translate(int xN[4], int yN[4], int x[4], int y[4], int x1,int y1)
{
xN[0]=x[0]+x1;
xN[1]=x[1]+x1;
xN[2]=x[2]+x1;
xN[3]=x[3]+x1;
yN[0]=y[0]+y1;
yN[1]=y[1]+y1;
yN[2]=y[2]+y1;
yN[3]=y[3]+y1;
}
void CTranslateView::drawRect(int x[4 ], int y[4], CDC *pDC)
{
pDC->MoveTo(x[0],y[0]);
pDC->LineTo(x[1], y[1]);
pDC->MoveTo(x[1],y[1]);
pDC->LineTo(x[2], y[2]);
pDC->MoveTo(x[2],y[2]);
pDC->LineTo(x[3], y[3]);
pDC->MoveTo(x[3],y[3]);
pDC->LineTo(x[0], y[0]);
}

Output:

Lab Task:
Q1: Draw a triangle and then translate it by a factor of 50 and color their boundary.

Source Code:
CMFCApplication4View.h

// Operations
public:
int x[3],y[3];
int xN[3],yN[3];

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

// Implementation
public:
virtual ~CMFCApplication4View();
void drawRect(int x[3],int y[3],CDC*);
void translate(int xN[3],int yN[3],int x[3], int y[3],int x1,int y1);

CMFCApplication4View.cpp

void CMFCApplication5View::OnDraw(CDC* pDC)


{
CMFCApplication5Doc* pDoc = GetDocument();
x[0]=125;
y[0]=10;
x[1]=95;
y[1]=70;
x[2]=155;
y[2]=70;

drawRect(x,y,pDC);
translate(xN,yN,x,y,50,50);
drawRect(xN,yN,pDC);

CPen penGreen(PS_SOLID,5,RGB(255,0,0));
CPen*pOldPen=NULL;
pOldPen=pDC->SelectObject(&penGreen);

pDC->MoveTo(x[0],y[0]);
pDC->LineTo(x[1],y[1]);
pDC->SelectObject(pOldPen);

CPen penGr(PS_SOLID,5,RGB(255,0,0));
CPen*hOldPen=NULL;
hOldPen=pDC->SelectObject(&penGr);
pDC->MoveTo(x[1],y[1]);
pDC->LineTo(x[2],y[2]);
pDC->SelectObject(hOldPen);

CPen penGre(PS_SOLID,5,RGB(255,0,0));
CPen*tOldPen=NULL;
tOldPen=pDC->SelectObject(&penGre);
pDC->MoveTo(x[2],y[2]);
pDC->LineTo(x[0],y[0]);
pDC->SelectObject(tOldPen);

ASSERT_VALID(pDoc);

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

if (!pDoc)
return;

// TODO: add draw code for native data here


}

// CMFCApplication5View printing

void CMFCApplication5View::translate(int xN[3],int yN[3],int x[3], int y[3],int x1,int y1){


xN[0]=x[0]+x1;
xN[1]=x[1]+x1;
xN[2]=x[2]+x1;
/*xN[3]=x[3]+x1;*/
yN[0]=y[0]+y1;
yN[1]=y[1]+y1;
yN[2]=y[2]+y1;
/*yN[3]=y[3]+y1;*/
}
void CMFCApplication5View::drawRect(int x[3],int y[3],CDC*pDC)
{

CPen penGreen(PS_SOLID,5,RGB(0,0,255));
CPen*pOldPen=NULL;
pOldPen=pDC->SelectObject(&penGreen);
pDC->MoveTo(x[0],y[0]);
pDC->LineTo(x[1],y[1]);
pDC->SelectObject(pOldPen);

CPen penGr(PS_SOLID,5,RGB(0,0,255));
CPen*hOldPen=NULL;
hOldPen=pDC->SelectObject(&penGr);

pDC->MoveTo(x[1],y[1]);
pDC->LineTo(x[2],y[2]);
pDC->SelectObject(hOldPen);

CPen penGre(PS_SOLID,5,RGB(0,0,255));
CPen*tOldPen=NULL;
tOldPen=pDC->SelectObject(&penGre);

pDC->MoveTo(x[2],y[2]);
pDC->LineTo(x[0],y[0]);
pDC->SelectObject(tOldPen);

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

Output:

Computer Engineering Department

You might also like