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. TED - September 5, 2010


    CheapTabletsOnline.Com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. Low price pills. Buy drugs online

    Buy:Retin-A.Human Growth Hormone.Prevacid.Petcam (Metacam) Oral Suspension.Zyban.Lumigan.Mega Hoodia.Valtrex.Prednisolone.Synthroid.100% Pure Okinawan Coral Calcium.Arimidex.Zovirax.Actos.Accutane.Nexium….

  2. watch movies online free - October 26, 2010

    Great Post!…

    [...] I found your entry interesting thus I’ve added a Trackback to it on my weblog :) [...]…

  3. Blog - October 27, 2010

    Blog…

    [...] something about blog[...]…

  4. reedem coupon platinium play - November 4, 2010

    slots free money sighn up bonus…

    jobs bel terra casino…

  5. casino 770 gratuit - November 4, 2010

    free slots java 247…

    free chip no deposit bonus sign up…

  6. eths soma - November 4, 2010

    lisinopril with soma…

    lisinopril with soma…

  7. canadian rxs soma - November 4, 2010

    cheap soma injection…

    canadian rxs soma…

  8. buy soma without a prescription - November 4, 2010

    soma watson brand…

    buy prescription soma online…

  9. valtrex dose for shingles - November 4, 2010

    valtrex and abreva…

    valtrex discount…

  10. home remedy for viagra - November 4, 2010

    viagra at walgreens…

    best buy viagra…

  11. download jackpot party - November 4, 2010

    play free casino games for fun…

    playtech casinos no deposit required…

  12. gratis videoslots no sign up no download - November 4, 2010

    hampton casino bonus code…

    rtg instant play no deposit bonus…

  13. what is tramadol hci - November 4, 2010

    tramadol oral…

    tramadol orders cod delivery companies…

  14. bonus codes for cyberbingo - November 4, 2010

    free slots download…

    vidio casino…

  15. buy viagra soft in england - November 4, 2010

    viagra 100mg generic…

    viagra versand…

  16. slots - November 4, 2010

    free promotion codes on casinos…

    foxwoods casino bonusplay…

  17. union plaza casino playing cards - November 4, 2010

    jackpot capitol free no deposit codes…

    new no deposit bonus casinos…

  18. buy 100mg tramadol - November 4, 2010

    is tramadol a pain killer…

    tramadol and lunesta…

  19. buy tramadol online now - November 4, 2010

    tramadol overnight delivery no perscription requir…

    tramadol long term damage…

  20. free super jackpot party slots - November 4, 2010

    grand casino tunica…

    clearwater casino washington state…

  21. casino en ligne en france - November 4, 2010

    instant pokerboni…

    play jack pot party slot free…

  22. casino online royal - November 4, 2010

    online casino mit startguthaben…

    roulette casino game…

  23. tramadol menstrual cramps - November 4, 2010

    tramadol vs vicadin…

    makr of tramadol…

  24. hard rock hotel and casino las vegas - November 4, 2010

    free slots machine ca…

    bingo free deposit…

  25. new casinos online with no deposit - November 4, 2010

    free signup poker bonus…

    casino royale for ipod torrent…

  26. where can i purchase valtrex - November 4, 2010

    where can i purchase valtrex…

    where can i purchase valtrex…

  27. slute load - November 4, 2010

    free betting money…

    new online casino bonus codes…

  28. casino free coupon codes - November 4, 2010

    free money bingo…

    free casino codes coupons june july august…

  29. soma without prescription overnight shipping - November 4, 2010

    cheap soma overnight…

    soma online cash on delivery…

  30. free play mega jack - November 4, 2010

    free casino game…

    minesota casinos…

  31. wms online free slots - November 4, 2010

    nodepositcasinobonus codes…

    play twice the honey casino game on-line…

  32. super jackpot party - November 4, 2010

    viplounge no deposit codes…

    2010 rtg no deposit codes…

  33. seminole hard rock hotel casino - November 4, 2010

    sun palace casino…

    no deposit casino promo codes free cash 2010…

  34. bingo star download - November 4, 2010

    kasyno online darmowy bonus…

    freeonlineslots lasvas…

  35. poker sites that accept cirrus - November 4, 2010

    new casinos free…

    casino crush fourm no deposit…

  36. valtrex voucher - November 4, 2010

    generic valtrex no prescription…

    valtrex in third trimester…

  37. play at free online flash play casinos slot machine in the usa - November 4, 2010

    casino game online…

    the best area to play in the casino…

  38. uk soma suppliers - November 4, 2010

    soma with overnight fedex…

    soma experiences…

  39. viagra label - November 4, 2010

    cialis over the counter america…

    viagra suppositories…

  40. ordering soma online no membership overnight delivery - November 4, 2010

    buy soma pill online…

    soma c.o.d….

  41. fedex overnight soma - November 4, 2010

    geneic soma…

    medicine soma…

  42. no download slot machine games - November 4, 2010

    new redeem coupon rtg…

    online casino games free play…

  43. viagra acne - November 4, 2010

    milfsex stories…

    bingo buy game online viagra…

  44. buy tramadol online cod buy ultram - November 4, 2010

    6 7 dihydroxybergamottin and tramadol…

    tramadol and methadone…

  45. mexican tramadol 100mg - November 4, 2010

    order tramadol online using checking account…

    amiodarone tramadol interaction…

  46. what is the medicine tramadol - November 4, 2010

    snorting dilaudid and tramadol…

    tramadol online shipped by ups…

  47. microgamingfree casinogames - November 4, 2010

    microgaming no deposit bonus…

    casino games that i can play offline…

  48. jind mahi lyrics tramadol - November 4, 2010

    jind mahi lyrics tramadol…

    help i am withdrawing from tramadol…

  49. generic viagra scams - November 4, 2010

    viagra glasgow…

    viagra recreational use…

  50. soma no prescription cod - November 4, 2010

    buy pill prescription soma without…

    buy pill prescription soma without…

Leave a Reply


*