0% found this document useful (0 votes)
34 views7 pages

Cgam Lab 5 59

The document describes an exercise to implement 2D rotation of shapes using drawRect() in C++. It includes code to: 1. Define functions for rotating an object and drawing a rectangle 2. Draw a rectangle, rotate it at different angles, and draw the rotated rectangles 3. Draw a triangle, rotate it at different angles, and draw the rotated triangles The tasks are completed by calling the rotation and drawing functions, setting coordinates for the shapes, and selecting pens of different colors for the boundaries.

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)
34 views7 pages

Cgam Lab 5 59

The document describes an exercise to implement 2D rotation of shapes using drawRect() in C++. It includes code to: 1. Define functions for rotating an object and drawing a rectangle 2. Draw a rectangle, rotate it at different angles, and draw the rotated rectangles 3. Draw a triangle, rotate it at different angles, and draw the rotated triangles The tasks are completed by calling the rotation and drawing functions, setting coordinates for the shapes, and selecting pens of different colors for the boundaries.

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/ 7

Computer Graphics Animation & Multimedia 2020-CE-059

LAB#5
Objective:
Implement 2D Transformation Rotation Function with drawRect()
Exercise:

Source Code:
MFCApplication3View.h:
public:
CMFCApplication3Doc* GetDocument() const;

// Operations
public:
int x[4], y[4];
int xN[4], yN[4];
float theta;

/ Implementation
public:
virtual ~CMFCApplication3View();
void drawRect(int x[4 ], int y[4], CDC*);
void RotateObject(int [4],int [4], int [4], int [4], double Angle);

MFCApplication3View.cpp
void CMFCApplication3View::OnDraw(CDC* pDC)
{
CMFCApplication3Doc* 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);
RotateObject( xN, yN, x, y, 30.0);
drawRect(xN,yN, pDC);
RotateObject( xN, yN, x, y, 45.0);
drawRect(xN,yN, pDC);
RotateObject( xN, yN, x, y, 120.0);
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

ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: add draw code for native data here


}
void CMFCApplication3View::RotateObject(int xN[4],int yN[4], int x[4], int y[4], double
Angle)
{
Angle= (Angle*3.14)/180;
xN[0]=x[0]*cos(Angle) - y[0]* sin(Angle);
yN[0]=x[0] * sin(Angle) + y[0] *cos(Angle);
xN[1]=x[1]*cos(Angle) - y[1]* sin(Angle);
yN[1]=x[1] * sin(Angle) + y[1] *cos(Angle);
xN[2]=x[2]*cos(Angle) - y[2]* sin(Angle);
yN[2]=x[2] * sin(Angle) + y[2] *cos(Angle);
xN[3]=x[3]*cos(Angle) - y[3]* sin(Angle);
yN[3]=x[3] * sin(Angle) + y[3] *cos(Angle);
}
void CMFCApplication3View::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:

Tasks:
1. Draw a Round Rectangle and then rotate it by different angles. and color their boundary.
2. Draw a triangle and then rotate it by different angles. and color their boundary

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

TASK 1:
MFCApplication3View.h

MFCApplication3View.cpp
#include "stdafx.h"
#include "math.h"

void CMFCApplication3View::OnDraw(CDC* pDC)


{
CMFCApplication3Doc* pDoc = GetDocument();
x[0]=350;
y[0]=75;
CPen penRED(PS_SOLID,5,RGB(255,0,0));
CPen*pOldPen=NULL;
pOldPen=pDC->SelectObject(&penRED);

x[1]=350;
y[1]=55;
x[2]=410;
y[2]=50;
x[3]=410;
y[3]=105;

pDC->RoundRect(x[0],y[0],x[2],y[2],32,32);
pDC->SelectObject(&penRED);
drawRect(x,y, pDC);
RotateObject( xN, yN, x, y, 59.0);
drawRect(xN,yN, pDC);
RotateObject( xN, yN, x, y, 99.0);
drawRect(xN,yN, pDC);
RotateObject( xN, yN, x, y, 40.0);
drawRect(xN,yN, pDC);
pDC->RoundRect(x[0],y[0],x[2],y[2],32,32);

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: add draw code for native data here


}
void CMFCApplication3View::RotateObject(int xN[4],int yN[4], int x[4], int y[4], double
Angle)
{
Angle= (Angle*3.14)/180;

xN[0]=x[0]*cos(Angle) - y[0]* sin(Angle);


yN[0]=x[0] * sin(Angle) + y[0] *cos(Angle);
xN[1]=x[1]*cos(Angle) - y[1]* sin(Angle);
yN[1]=x[1] * sin(Angle) + y[1] *cos(Angle);
xN[2]=x[2]*cos(Angle) - y[2]* sin(Angle);
yN[2]=x[2] * sin(Angle) + y[2] *cos(Angle);
}
void CMFCApplication3View::drawRect(int x[3], int y[3], CDC *pDC)
{

pDC->RoundRect(x[0],y[0],x[2],y[2],32,32);

OUTPUT:

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

TASK2:
MFCApplicationView.h:

MFCApplication3View.cpp:
#include "stdafx.h"
#include "math.h"

void CMFCApplication3View::OnDraw(CDC* pDC)


{
CMFCApplication3Doc* pDoc = GetDocument();
x[0]=220;
y[0]=120;
x[1]=220;
y[1]=50;
x[2]=290;
y[2]=50;
drawRect(x,y,pDC);
RotateObject( xN, yN, x, y, 59.0);
drawRect(xN,yN, pDC);
RotateObject( xN, yN, x, y, 90.0);
drawRect(xN,yN, pDC);
RotateObject( xN, yN, x, y, 32.0);
drawRect(xN,yN, pDC);

CPen penRed(PS_SOLID, 5, RGB(255,0 ,0));


CPen* pOldPen=NULL;
pOldPen=pDC->SelectObject(&penRed);
pDC->MoveTo(x[0],y[0]);
pDC->LineTo(x[1], y[1]);
pDC->SelectObject(pOldPen);

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

CPen penReed(PS_SOLID, 5, RGB(255,0 ,0));


CPen* hOldPen=NULL;
hOldPen=pDC->SelectObject(&penRed);

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

CPen penRedd(PS_SOLID, 5, RGB(255,0 ,0));


CPen* zOldPen=NULL;
zOldPen=pDC->SelectObject(&penRed);

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

ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: add draw code for native data here


}
void CMFCApplication3View::RotateObject(int xN[4],int yN[4], int x[4], int y[4], double
Angle)
{
Angle= (Angle*3.14)/180;

xN[0]=x[0]*cos(Angle) - y[0]* sin(Angle);


yN[0]=x[0] * sin(Angle) + y[0] *cos(Angle);
xN[1]=x[1]*cos(Angle) - y[1]* sin(Angle);
yN[1]=x[1] * sin(Angle) + y[1] *cos(Angle);
xN[2]=x[2]*cos(Angle) - y[2]* sin(Angle);
yN[2]=x[2] * sin(Angle) + y[2] *cos(Angle);
}
void CMFCApplication3View::drawRect(int x[3], int y[3], CDC *pDC)
{
CPen penRed(PS_SOLID, 5, RGB(255,0 ,0));
CPen* pOldPen=NULL;
pOldPen=pDC->SelectObject(&penRed);

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

CPen penRed(PS_SOLID, 5, RGB(255,0 ,0));


CPen* hOldPen=NULL;
hOldPen=pDC->SelectObject(&penRed);

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

CPen penRed(PS_SOLID, 5, RGB(255,0 ,0));

Computer Engineering Department


Computer Graphics Animation & Multimedia 2020-CE-059

CPen* zOldPen=NULL;
zOldPen=pDC->SelectObject(&penRed);

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

OUTPUT:

Computer Engineering Department

You might also like