functions and movement

We have been assigned to make the objects in our previous homework rotate and move downward on the screen. I know I need to create functions for the rotation of the stars, as well as the movement downwards. I am really lost on where to begin with these functions. I do know that the movement must be related to the time (t) in seconds that have passed. Please note that the draw triangle function is part of a previous assignment and we are required not to modify it in any way.

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
// Directives:
#include <iostream>
#include <cstdlib>
#include <graphics.h>
#include <cmath>
using namespace std;

// Named constants:
const int S = 500; // Width and height of graphics window
 
// Prototypes:
void draw_triangle(int x, int y, int d, double theta);

// Function definitions:

int main()
{
// 1. Initialize the graphics window:
initwindow(S, S, "Canis Major", 0, 0, true);
while(shift < 0.8*S)
{
shift = 30*t;     // Calculations
clearviewport();  // Clear screen
swapbuffers();    // Swap buffers
}

// 2. Draw triangles:
draw_triangle(int(S*3/5), int(S*1/5), int(.056*S), double(M_PI/2));   
draw_triangle(int(S*1/3), int(S*5/6), int(.048*S), double(M_PI/2));   
draw_triangle(int(S*9/10), int(S*3/10), int(.04*S), double(M_PI/2));  
draw_triangle(int(S*2/9), int(S*9/12), int(.032*S), double(M_PI/2));   
draw_triangle(int(S*1/15), int(S*8/9), int(.024*S), double(M_PI/2)); 
// Draw upside-down triangles
draw_triangle(int(S*3/5), int(S*1/5), int(.056*S), double(-M_PI/2));   
draw_triangle(int(S*1/3), int(S*5/6), int(.048*S), double(-M_PI/2));    
draw_triangle(int(S*9/10), int(S*3/10), int(.04*S), double(-M_PI/2));  
draw_triangle(int(S*2/9), int(S*9/12), int(.032*S), double(-M_PI/2));  
draw_triangle(int(S*1/15), int(S*8/9), int(.024*S), double(-M_PI/2));

    // 3. Finish:
    delay(20000);
    return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------
void draw_triangle(int x, int y, int d, double theta)
{
    int x1 = int(x + d * cos(theta));
    int x2 = int(x + d * cos(theta - 2* M_PI / 3));
    int x3 = int(x + d * cos(theta + 2* M_PI / 3));
    int y1 = int(y - d * sin(theta));
    int y2 = int(y - d * sin(theta - 2* M_PI / 3));
    int y3 = int(y - d * sin(theta + 2* M_PI / 3));

    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);
}

I do not expect anyone to do this assignment for me, I'm just looking for advice/explanation.
Thank you, this is only my second program so any advice is much appreciated!
Last edited on
Basically, just call the draw_triangle function with different parameters. The rest is just trigonometry and other basic math stuff. I personally would start by organizing the data in structs or classes, but I don't know how far you are into the course yet.
Last edited on
Thank you. I know that I have to have a single animation loop for the movement but I'm not sure how to do that.

The main program must have a single animation loop with double-buffering.

Compute values that are needed for variables such as shift and tilt (and a time value, t, if you wish).
So I need to create two functions, just not sure where to begin with them.

Clear the screen and draw the next frame of the animation.

Swap buffers and delay for 1/30th of a second.

The job of drawing the entire constellation must be done by a function with two parameters called tilt (or some other name if you prefer) and shift. The value of tilt is the total rotation (in radians) that the stars have spun so far from their starting position. The value of shift is the total amount (in pixels) that the stars have dropped so far from their starting position.
I wish I could help you more, but it's just that that code isn't very clear in what it is doing. For example, how is the triangle drawn? If I were to write a function that draws a triangle I would just write a function that takes 3 points, the one you have there seems unnecessarily complicated to me.
I agree that the triangle function is complicated, but this was how the professor required us to write it. It would be helpful to know how to make objects move or rotate so any information you have on that would be useful.
Again, I am not sure how this triangle function actually works. It seems to me like it's creating a equilateral triangle, with d being the side length, x and y marking a starting point and theta being some angle by which the triangle is rotated, is that correct?

Intuitively I would say that moving (shifting) the triangle should be done by changing the x/y parameters, and rotation should be done by changing the theta parameter. That being said, I am not sure how this function actually operates (like what is the center of the rotation), and if you want to rotate shapes you will probably have to change the x/y values for the rotation as well. That's why I said needlessly complicated - it seems to me your professor wanted to reduce the number of arguments passed to the function, but in turn made the function less powerful (can only draw one kind of triangle) and much less intuitive to use. And also made the function do more than one thing, which is an anti-pattern.
Topic archived. No new replies allowed.