I have a school project atm. and me including the whole class + teacher has no idea how the hell to do it, so here is what we need:
We need to somehow create a triangle, then make the triangle rotate and then move the geometric center of the trinagle around.
We have no idea, no skills, and have barely a simple idea how for/do loops work, so please don't asume us to know some stuff, I would really appreciate your help :)
Some questions/comments first, because you haven't defined your problem sufficiently.
- Are you going to use library functions (for example to draw the triangle, or do the rotation by a function call) ... or are you going to define the triangle vertices and use mathematics/geometry to calculate the individual points. The rest of this post assumes the latter. If you are just going to use inbuilt library routines then it will be irrelevant.
- Define a triangle: i.e. input its 3 vertices (x1,y1), (x2,y2), (x3,y3)
- Calculate its geometric centre - just average each of the x and y vertex values:
xav = (x1+x2+x3)/3 and similarly for yav.
Easy to write a function to do this.
- A rotation is defined by (i) the centre of rotation (x0,y0) and (ii) the angle rotated (anticlockwise) about it. (Formula later, below, so you can write a function to do this for each vertex.)
- A translation is defined by (a,b), where a is the distance moved in the x direction and b is the distance moved in the y direction. So, for the first vertex (x1,y1) simply becomes (x1+a,y1+b) and similarly for the other vertices. Again, you could write a function to do this for each vertex.
If you are going to do it manually then a rotation of angle A about the ORIGIN (0,0) changes (x,y) as follows
to
xnew = x cos(A) - y sin(A)
ynew = x sin(A) + y cos(A)
If you rotate about some other point (x0,y0) instead then
xnew = x0 + (x-x0) cos(A) - (y-y0) sin(A)
ynew = y0 + (x-x0) sin(A) + (y-y0) cos(A)
If you use most programming languages' cos and sin routines then angle A will have to be in radians to apply them.
struct Point
{
int X, Y;
Point(int x, int y)
{
X = x;
Y = y;
}
Point()
{
X = Y = 0;
}
};
struct Triangle
{
Point A, B, C;
Triangle(const Point& a, const Point& b, const Point& c)
{
A = a;
B = b;
C = c;
}
void rotate(double amount)
{
// your code here
}
Point get_center()
{
Point temp;
// calculate and set the X and Y value of temp
return temp;
}
void draw( /* some drawing area */)
{
// your drawing code here
}
};
I have a school project atm. and me including the whole class + teacher has no idea how the hell to do it, so here is what we need:
Sorry, but isn't that kinda retarded? Why would a teacher give an assignment he can't do? As for drawing a triangle there are lots of ways, lots of frameworks. For example this is the snippet for drawing a triangle in WinApi (windows graphic interface)
There's also graphic frameworks like DirectX and OpenGL that work mainly with triangles, but we're already way too far from a beginner's expertise here (even WinApi is)
Hey ppl
Thanks for answers and help :)
What graphics library? Whatever you suggest
---------------------------
Whatever we can pull off we're basically trying everything we can find :/ Gonna test it tomorrow.
--------------------------
How can we start shuch a project without skills? Idk man^^ we went from basic calculation to "I want a triangle and it should rotate and I want it moving" thx for the code suggestion, but I'd like to know wich libraries are you using :)
--------------------------
Isnt that kinda retarded? Oh yes sir it is^^ also thx for the code same as post above wich librariesare you using?
Thomas's code is a commented-style sudo-code, it defines a triangle but leaves the drawing to whatever gui you decide to plug in later, Golden Lizard's code is windowsAPI and is dealing with the drawing portion of creating a triangle on the screen using three points (though he leaves out all code dealing with opening and managing the window).
Basically they are giving you stuff to get you started, but you aren't getting wholly working code. That isn't the point of this site (you could post code and ask what you're doing wrong, but don't expect to get full code without putting in the effort).
I know you said you're just getting into for loops, so I'm sad to say this project is currently out of reach.
I'm not saying it can't be done, just that you need to take a breather. Do some more basic research in the language before you tackle this. Then look into GUI libraries, there are quite a few; I tend towards SDL2 or Qt. I should say I found SFML to be one of the easiest to learn as GUI's go. All three have bindings in opengl.
This sounds like a simple deal, but you're talking about some advanced topics. I don't want to discourage you, but you're going to want to have understanding of pointers and objects at least before you move into GUI's. Since you're in (middle-school or high-school?); Depending on your learning curve and spare-time, this could take more time than you have available for the school year. If you can focus 24/7 on this one goal, then you might be able to get it done in a month or three.
Look up bucky's c++ tutorials on youtube to get a little help on the basic language at least before you move on.
Once you know about the basic transformations and matrix notation you can put it all together and display a (transformed) triangle as a simple set of 3 points i.e as a table of values or as a line/filled triangle in the form of a diagram on a graphic display (or both).
The key is the basic transform. The tool is matrices.
C++ is not for programming noobs, it's more difficult than say python / java / c# / visual basic etc. In those languages jumping into GUI is far more simple. What I would personally use is OpenGL, since working with triangles is the most basic thing that graphic cards can do (well, after vertices and lines) and rotate it by using a rotation matrix, but there are about 200 lines of code and vector field math separating you from that.
The thing that even the teacher can't do this is something very fishy. How do your colleagues do this? Have you spoken with any of them?
The next person who learns C++ starting from scratch will be a noob. The next person who makes a brand new discovery about some otherwise facet of human knowledge will be a noob. The next person or group of people who have the courage and determination to discover and adapt what is already there are noob heroes. Good on them all!
No, what I'm trying to say is that C++ has a steep learning curve. You need to take it slowly, other languages have a smoother progression and it's easier to jump into graphic interfaces and such. Yes, you can start with C++ too, but you will have an easier time learning it if you already know some easier language first. It's just that concepts like dynamic memory, ownership and referencing are harder to wrap your mind around, while languages that offer garbage collection are easier to start with.
Maybe they are all budding Nobel winners, which I hope they are. If they aren't, they'll be like the rest of us, noobs at something if not most things.
@newbieg taking a breather is not an option. I think you misunderstod something it's what we have to do to pass. What we have done so far is a 1st: program that tells you the multiplication table of a number you enter. 2nd: a A•X=B Matrix calculator with Kramer method 3rd: A prime number generator.
That's all and the triangle shit is what he wants next we have like 2-3 weeks left, so cool I get what the other codes are for but if I have no idea how to get them working so I can do something with it I have to ask, what I did. It may be out of our range BUT FML somehow we need to get this shit done...
As @Keskiverto pointed out earlier, to "have" a triangle doesn't necessarily mean you have to "draw" it - you just have to state what happens to 3 points under rotation, followed by translation. i.e.
Input 3 points.
Rotate them (presumably about their average).
Translate them.
That isn't actually asking very much. Do it by hand with a calculator, note what you do, put it in code.
@Thomas1965 has given you an excellent template, with constructors for Point and Triangle in place and clear instructions as to where to put very small amounts of code in the member functions. The only thing I might add was a "translate" member function to match the "rotate" one, and maybe put similar functions in the Point structure. Once you have filled in the very small number of "action" lines in the Point and Triangle structures (or classes) all you have to do is call the constructors and member functions of "Triangle" and you are done.
If you want to draw the triangle that's a bonus ... but it isn't strictly necessary.
@Denix, maybe you could provide more information, so we can guess what you actually have to do. So you got some university assignment, but at what course? Procedural Programming? Introduction to programming? Maybe you could just map some points on a 2d array and then move their position ("rotating") and it would be enough. Maybe you just need to calculate the point's position after rotating like @lastchance said.
You said you made a matrix calculator, so rotating a triangle make much more sense now. I'm sure you must have taken some vector math courses in your university, so it shouldn't be too hard to apply what you have learned there to rotate a triangle.
we're not in university^^ so no vectro math courses just my 12th grade teacher talking shit he doesn't even know how to do^^
In what you asked, Introduction to programming is what comes the closest, the matrix calculator hasn't any purpose and its just stupid adding and multiplying the different determinants so nothing that we would think will help us here, if you want tomorrow at class I can pass you the code. And I'm totaly impressed by you guys here but how do we rotate the points; yes I know it's like a lil kid asking how to kill a mammoth;
Well for now we have a triangle with 3 separate lines, we can move it in lines, but bo rotation so far, gonna add the code we have in here asap
THX so much for help guys lets see how far my mates and me can get with the new entries here :)
Rotating the triangle is simply rotating the 3 points - the 3 vertices. Everything else - lines, fill, patterns all follow from that basic idea. you can move the triangle in lines if you like but that's not the easiest way.
but how do we rotate the points
A point rotation is shown to you in a couple of posts above. A line is just the join between 2 rotated points. Provided each point has the same rotation transformation applied to it, the rotated triangle won't be distorted.
Good luck with your coding. Hope you decide to post it here some day ...