Moving triangle

Hi ppl

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 :)
What graphics library?
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.
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

How can you start such a project without skills??
Maybe you can learn all the skills on the way.

Kind of starting point - not detailed waterproof design
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case WM_PAINT:
{
  PAINTSTRUCT ps;
  HDC hdc=BeginPaint(handle,&ps);
	
  TextOut(hdc, 0, 0, L"Triangle", lstrlenW(L"Triangle"));


  POINT pts[] = {point1.x,point1.y, 
                 point2.x,point2.y, 
                 point3.x,point3.y};

  Polyline(hdc, pts, 6);
		 
  EndPaint(handle,&ps);
  return 0;
}


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)
Last edited on
Pedantically, to have a triangle does not require that you draw anything.
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?

Thx for the help guys :)
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.
Last edited on
Since "Qt" and "OpenGL" were mentioned ... http://doc.qt.io/qt-5/qtgui-openglwindow-example.html
closed account (48T7M4Gy)
www.cs.brandeis.edu/~cs155/Lecture_06.pdf

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?
closed account (48T7M4Gy)
C++ is not for programming noobs


Really?

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!

There's nothing fishy about that!
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.
Last edited on
closed account (48T7M4Gy)
C++ has a steep learning curve

Why not just leave it at that?

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...
Denix

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.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cmath>
#include <iomanip>

struct Point{
    double x = 0;
    double y = 0;
    
    Point(double aX = 0, double aY = 0){
        x = aX;
        y = aY;
    }
    
    void translate( double aX, double aY ){
        x += aX;
        y += aY;
    }
    // ANTI-CLOCKWISE ROTATION OF Point ABOUT ORIGIN
    void rotate( double aAngle ){
        double xTemp = x;
        double yTemp = y;
        
        double theta = aAngle * M_PI / 180;
        double c = cos( theta );
        double s = sin( theta );
        
        x = xTemp * c - yTemp * s;
        y = xTemp * s + yTemp * c;
    }
};

std::ostream & operator<<(std::ostream & out, const Point & aPoint){
    out << std::fixed << std::setprecision(2) << "(" << aPoint.x << ", " << aPoint.y << ")";
    return out;
}

int main(){
    
    Point triangle[] = {Point(2,1), Point(3,3), Point(4,2)};
    Point geometric_center(0,0);
    
    for(auto i:triangle){
        geometric_center.x += i.x/3;
        geometric_center.y += i.y/3;
    }
    std::cout << "Geometric center: " << geometric_center << '\n';
    
    for(auto i:triangle){
        std::cout << i << " -> ";
        i.translate(-geometric_center.x, -geometric_center.y);
        i.rotate(90);
        i.translate(geometric_center.x, geometric_center.y);
        
        std::cout << i << '\n';
    }
    return 0;
}

Geometric center: (3.00, 2.00)
(2.00, 1.00) -> (4.00, 1.00)
(3.00, 3.00) -> (2.00, 2.00)
(4.00, 2.00) -> (3.00, 3.00)
Program ended with exit code: 0
Last edited on
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 :)
closed account (48T7M4Gy)
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 ...
Topic archived. No new replies allowed.