Archive for December, 2009
Happy New Year.
1
Because tomorrow I’ll go somewhere in vacation, and I’m not sure that I’ll have an internet connection there, I decided to wish you now a happy new year and may all your dreams come true. Nothing more to say, I have to pack my things. So, for the next year I will be back with updates and a new tutorial, like I promised. And by the way, my phone will probably be down. So this is my time to rest, don’t disturb me please.
Happy New Year !!!
Coming soon
1With this post I’ll keep you in touch with my work, right now I’m working on a project for university. About this project I can tell you that, it is somehow like the last project, but this one is working with 3D Objects and is made in C# with GDI+. This week I’ll finish it and after that I’ll post it here and I’ll make a little tutorial how it works. After that I’ll be very busy with the exams, but this week I’ll come with a some posts to update you about my projects and after those exams I’ll have another working session at a new project. So I’ll have a lot of work and I’ll keep you in touch.
Online contest
3My girlfriend just participated at an online contest with recipes for cabbage rolls and these days has ended. And in the last day of the contest I decided to monitorize the votes of each competitor while my girlfriend tried to make more votes. This took me a while, but I saw something interesting, I saw how this online contest are so stupid organized. It was so clear that there were hundreds of fake votes. And another thing, my girlfriend had an problem with the votes, how much she tried to increase her votes count, she couldn’t. Why ? I don’t know. So we got angry and we sent them from where they came. With all those maybe tehnical problems, I truly believe that all this contests are just fakes, and all the filters they had, to stop frauds, were for nothing. Because those filters helped frauds and stoped the fair ones, I mean the votes.
So I can advise you to think twice before you want to register in an online contest, because is just a waste of time. Maybe I’ll come later with another post with more details about this contest, I just have to think if I want to make them a bad review or not.
2D Object Manipulation in C++
1690I 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.
|
|
download: 2D Object Manipulation (1.28KB) added: 18/12/2009 clicks: 325 description: open source code for 2d object manipulation |
Boring days
2Last two days for me were so boring, I just did not feel like doing anything. I’ve got some projects for the university, but I had no motivation to start, even at all. I hope this week will be slightly better, and over the weekend I can go to Avatar, at best case, if is going to appear so fast in my city because I am not in the capital.
So see you soon with another post longer, because now I really do not feel like writing.
And I’ll show you the project at wich I’m working, is something simple and trivial, but still believe there are many who would need an example like this.