2D Object Manipulation in C++

I did that project I was talking about and I submitted for record too, so now I can show what was about. Is a simple program made in C++ wich have an object and you can handle it. You can move it left, right, up, down, you can scale it, you can rotate it after the left up corner, you can make his projections on both axes and you can shear it. About the method used to implement this I can tell you that I have used a class with methods, and the object points are read from another file. This should be a basic tutorial for those who are new using classes and 2D object manipulation. So lets start with the code. Text followed by // is a comment.

POINTS.IN //input file with points

20 20 // left top point
50 50 // right bottom point

PROJ2D.CPP


//GLOBAL VARIABLES
FILE *f;
int inData[20];

//DEFINITION OF CLASSES AND STRUCTURES
typedef struct Point {
float x,y;
};

class Object{
public: Point punct[10];
//CLASS PROPERTIES
float points, move, scale, angle, forf;
double aux_x;
//METHODS
~Object(); // DESTRUCTOR
Object(); //CONSTRUCTOR
void ReadPoints();
void Draw();
void Move(char);
void Scale(int);
void Rotate(float);
void Shear(float);
void Projection(char);
};

How you can see above we have defined our variables, our structer named Point and the class named Object. In the Object class we have defined our class properties or variables, how you like to say it and the methods wich we are going to write them out of the class. Now we are going to write the constructor and destructor for our class.

Object::Object()
{
ReadPoints();
move=2;
scale=1.1;
angle=0.03;
forf=0.03;
}

Object::~Object()
{
delete &punct;
delete &points;
delete &move;
delete &scale;
delete ∠
delete &forf;
delete &aux_x;
}

How you can see our methods is like a function with the same name as the Class, but you can see that in front of the method name is the class name, that means class Object is the parent of that method. So in our constructor we assign values to our class properties and in the destructor we delete those variables to free our memorie. So next we are going to read our points from the input file.

void Object::ReadPoints()
{
points=0; //points counter
f=fopen(“points.in”,”r”);
while(!feof(f))
{
points++;
fscanf(f,”%f %f”,&punct[points].x, &punct[points].y);
}
fclose(f);
}

This method should be very simple, we open our input file to read it, and we get all points in the variable punct in the right coordonates (x and y). After that we can draw our object line by line, just like you can see in the next method.

void Object::Draw()
{
for(int i=1;i

{
line(punct[i].x, punct[i].y, punct[i+1].x, punct[i+1].y);
}
line(punct[1].x, punct[1].y, punct[i].x, punct[i].y);
}

About the other methods I don’t have what to tell you about, they are using some mathematical functions and if you have some basic knowledge about programing, you wont have any problems. At the rotation method we apply a mathematical function for x coordonate and another mathematical function for y coordonate, for each point of the object.

void Object::Rotate(float side)
{
float rad = M_PI/180;
float angle = side*rad;

for(int i=1;i<=points;i++)
{
aux_x=(double)(punct[i].x*cos(angle))-(double)(punct[i].y*sin(angle));
punct[i].y=(double)(punct[i].x*sin(angle))+(double)(punct[i].y*cos(angle));
punct[i].x=aux_x;
}
cleardevice();
Draw();
}

At the Shear method we have a mathematical function to move the object’s points, depending on what direction we want to shear and using our forf parameter.

void Object::Shear(float side)
{
int x,y;
for(int i=1;i<=points;i++)
{
x=punct[i].x;
y=punct[i].y;

if(side==1)
punct[i].x=x+forf*y;
if(side==-1)
punct[i].y=y+forf*x;
if(side==2)
punct[i].x=x-forf*y;
if(side==-2)
punct[i].y=y-forf*x;
}
cleardevice();
Draw();
}

At the Move method we just have to see what in direction we want to move it, and after that we increment x or y or we decrement it, depending on the side.

void Object::Move(char side)
{
if(side==’W')
for(int i=1;i<=points;i++)
punct[i].y-=move;
if(side==’A')
for(int i=1;i<=points;i++)
punct[i].x-=move;
if(side==’S')
for(int i=1;i<=points;i++)
punct[i].y+=move;
if(side==’D')
for(int i=1;i<=points;i++)
punct[i].x+=move;
cleardevice();
Draw();
}

If we want to zoom in we have to multiply the coordonates with the scale parameter. And if we want to zoom out just divide the coordonates by the scale parameter.

void Object::Scale(int fs)
{
if(fs>0)
{
for(int i=1;i<=points;i++)
{
punct[i].x*=scale;
punct[i].y*=scale;
}
}
else
{
for(int i=1;i<=points;i++)
{
punct[i].x/=scale;
punct[i].y/=scale;
}
}
cleardevice();
Draw();
}

At projections we have to check on wich axes we want to make projections and after that the coordonates from the other axe we make them all zero;

void Object::Projection(char axis)
{
if(axis==’X')
for(int i=1;i<=points;i++)
{
punct[i].x*=1;
punct[i].y*=0;
}
else
for(int i=1;i<=points;i++)
{
punct[i].x*=0;
punct[i].y*=1;
}
cleardevice();
Draw();
}

What I should tell you is how you can handle the object. So lets see the main function.

void main()
{
//WE LOAD OUR GRAPHIC MODE
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, “C:\\BORLANDC\\BGI”);
errorcode = graphresult();
if(errorcode != grOk)
{
printf(“Graphics error: %s\n”, grapherrormsg(errorcode));
printf(“Press any key…”);
getch();
exit(1);
}
//MAIN
cleardevice();
Object a;
char key;
a.ReadPoints();
a.Draw();
do{
key=getch();
if(key==119) a.Move(‘W’); //move up
if(key==97) a.Move(‘A’); //move left
if(key==115) a.Move(‘S’); //move down
if(key==100) a.Move(‘D’); //move right
if(key==43) a.Scale(1); //zoom in
if(key==45) a.Scale(-1); //zoom out
if(key==60) a.Rotate(5); //rotate to left
if(key==62) a.Rotate(-5); //rotate to right
if(key==120) a.Shear(1); //right shear by x
if(key== 99) a.Shear(-1); //bottom shear by y
if(key==122) a.Shear(2); //left shear by x
if(key==101) a.Shear(-2); //upper shear by y
if(key==49) a.Projection(‘X’); //projection by x
if(key==50) a.Projection(‘Y’); //projection by y
if(key==114) { cleardevice(); a.ReadPoints(); a.Draw(); } //if you press r key you reset the object
}while(key!=27); // while we press ESC
closegraph(); //we close our graphic mode
}

So this is all, clean and simple, you can handle your own 2D object, try to make another object by writing another points in the input file, you can write as many points you want, but if you enter more than nine you should change in the class definiton.

http://blog.deddu.com/wp-content/plugins/downloads-manager/img/icons/winrar.gif download: 2D Object Manipulation (1.28KB)
added: 18/12/2009
clicks: 336
description: open source code for 2d object manipulation

, , , ,

Trackbacks/Pingbacks

  1. valtrex kidney stones - December 23, 2010

    generic valtrex shortage…

    order valtrex online…

  2. order soma cash on delivery - December 23, 2010

    online prescription soma without…

    online soma dosage…

  3. valtrex cost cvs - December 23, 2010

    generic valtrex 2009…

    valtrex nausea…

  4. viagra amsterdam - December 23, 2010

    usps delivery viagra…

    cialis and viagra cocktail…

  5. valtrex savings card - December 23, 2010

    valtrex zovirax…

    valtrex online canada…

  6. microgaming flash casinos with instant no deposit bonuses - December 23, 2010

    no depoist online casino…

    reno hilton casino…

  7. cheap soma no prescription - December 23, 2010

    soma screening test…

    pill identification soma…

  8. can u take valtrex while pregnant - December 23, 2010

    valtrex dose for cold sores…

    valtrex online…

  9. valtrex tablets 500mg - December 23, 2010

    valtrex treatment for cold sores…

    valtrex kidney failure…

  10. online casino bonus - December 23, 2010

    cupom g fed casino…

    newest no deposit sign up bonus…

  11. casino en ligne canadien - December 23, 2010

    new no deposit casinosites in usa…

    casino minimum deposit…

  12. casino online perla - December 23, 2010

    on line free slot machines…

    redeem cupon viplounge…

  13. casino en ligne flash - December 23, 2010

    all slots microgaming free chip…

    online casino 1 stunde freispiel…

  14. casino online gratis tragamonedas - December 23, 2010

    play for fun slot sites…

    casino online gratis tragamonedas…

  15. casino online espaƱol - December 23, 2010

    free slot sites no deposit…

    online casino de…

  16. valtrex joint pain - December 23, 2010

    valtrex side effects liver…

    valtrex autism…

  17. cheapest tramadol next day delivery - December 23, 2010

    itching from tramadol…

    tramadol 200 mg order from spain…

  18. new us online casino no deposit bonus - December 23, 2010

    newest codes and bonus casino 2010…

    casino online book of ra…

  19. diamond run slots - December 23, 2010

    flash pokers no deposit…

    free online wms slot games to download…

  20. bodog no deposit casino code - December 23, 2010

    no deposit codes casino…

    armenian casino…

  21. amyl nz - December 23, 2010

    where can i buy herbal viagra…

    viagra speedos videos…

  22. soma pill - December 23, 2010

    soma pill…

    geniric soma…

  23. aura soma l - December 23, 2010

    cheap generic soma substitutes…

    soma no dr contact…

  24. valtrex side effects - December 23, 2010

    valtrex in children…

    valtrex and tylenol…

  25. valtrex for herpes outbreak - December 23, 2010

    valtrex genital herpes…

    valtrex dosage for herpes zoster…

  26. online pharmacy loratabs soma - December 23, 2010

    soma generico impotencia…

    fedex soma overnight without a prescription…

  27. $5 min deposit online bingo - December 23, 2010

    play free casino game…

    this is vegas new no deposit required bonus codes coupon 2010…

  28. order soma cheap no membership fees no prescription - December 23, 2010

    soma testimonials…

    generic somageneric soma…

  29. online casino kaufen - December 23, 2010

    sign up poker bonus forum 2010…

    cash crush…

  30. tramadol and muscel spasm - December 23, 2010

    prescription what is tramadol…

    tramadol 500mg information pharmacology…

  31. casino en ligne virtuel - December 23, 2010

    free online jackpot party slot…

    casino online francais…

  32. viagra verses cialas - December 23, 2010

    prescription free viagra amsterdam…

    discussing viagra…

  33. soma side affects - December 23, 2010

    soma shipped overnight no prescription…

    25 mg soma…

  34. online casino with free money and no deposit bonus - December 23, 2010

    free play 1 hour casino…

    william video slots for sale super jackpot party…

  35. tunica mississippi casino reviews - December 23, 2010

    phantom efx reel deal casino high roller download blogspot…

    tunica mississippi casino reviews…

  36. poker no deposit code - December 23, 2010

    free slots at foxwoods…

    bingo without depost…

  37. new no deposit party city - December 23, 2010

    jeux de casino…

    argosy casino…

  38. spintop games coupon free fun - December 23, 2010

    platinium play…

    microgaming bonus codes forums…

  39. reno casino hotel - December 23, 2010

    casino online autorizzati aams…

    the plaza casino las vegas…

  40. 1 hour free play no download - December 23, 2010

    pocono casino…

    free game on line…

  41. viagra tie - December 24, 2010

    viagra expensive…

    viagra sfo…

  42. no minimum deposit 25 cent blackjack usa - December 24, 2010

    golden nugget vip lounge…

    bookmaker no deposit required…

  43. super jackpot party slot free machine - December 24, 2010

    instant sign up bonus bingo games…

    all jackpots casino…

  44. new coupon codes for cirrus - December 24, 2010

    free cash no deposit roulette ladyluck…

    free cash no deposit roulette ladyluck…

  45. washington state indian casinos - December 24, 2010

    bonus casino codes 2010…

    no deposit playtech…

  46. generic valtrex dosage - December 24, 2010

    valtrex pregnancy…

    valtrex and weight gain…

  47. pachanga casino - December 24, 2010

    no deposit coupon codes 2010…

    play super jackpot party slots…

  48. casino online confiable - December 24, 2010

    no deposit casinos new…

    free instant bonus codes with no deposit required casinos…

  49. stirling moss soma - December 24, 2010

    funny picture soma…

    cheap soma no rx…

  50. thailand viagra without prescription - December 24, 2010

    buy viagra for women…

    online viagra order…

Leave a Reply


*