1) First of all, if the task says that the function has to accept the coordinates as ARGUMENTS, that means you have to pass those coordinates to the function from the outside, when you call the function. The showName function you wrote has no arguments.
Second, when you define a variable, you can initialize it at the same time, so instead of writing this:
int centerX;
centerX=width/2;
write this, next time:
But this is not even necessary, because you can calculate the values "on the fly", without storing the values in variables, if the code is simplified. This is my solution to your first task:
#include "DarkGDK.h"
void showName(int X, int Y)
{
//dbDot(X, Y);
dbCenterText(X, Y, "Name");
}
void DarkGDK()
{
showName(dbScreenWidth() / 2, dbScreenHeight() / 2);
dbWaitKey();
}
I commented out the dbDot because that is just for checking that the text is in the good place, it is not necessary to execute the function. Note the X and Y function arguments and how they are used when the function is called. Since the task only wants the function definition, the solution is only the function header and the one-line function body, but I wanted to show you how it's called from DarkGDK main program.
2) The second task is really easy, you should be able to figure it out on your own. The top corner is the X,Y position that the function receives as ARGUMENTS (see the previous example). If the height of the triangle is 50 pixels, then the Y coordinate of the other two points will be Y+50. The width of the triangle is not specified, so we can use any value we like. If the width is also 50, then the X coordinates of the other two points will be X-25, X+25. Connect the three points with dbLine.
First try to write it on your own, then check out the code below.
#include "DarkGDK.h"
void drawTriangle(int X, int Y)
{
dbLine(X, Y, X-25, Y+50);
dbLine(X, Y, X+25, Y+50);
dbLine(X-25, Y+50, X+25, Y+50);
}
void DarkGDK()
{
drawTriangle(dbScreenWidth() / 2, dbScreenHeight() / 2);
dbWaitKey();
}
3) For an octagon: Divide a circle (360 degrees) into 8 parts, what do you get? An angle of 45 degrees. Calculate the 8 points on the circle which are 45 degrees apart from each other and connect them. Four points are very easy to calculate, if the octagon is oriented so that one of the corner points is at the top. For the other four corners you need sine/cosine. But, since the sine and cosine of 45 degrees are equal, you only need one of them.
In the code below, the center point and the radius of the octagon are all arguments of the function, so it can be called with different values. Again, think about it on your own before you have a look at the solution.
#include "DarkGDK.h"
void drawOctagon(int X, int Y, int R)
{
int Diff = int(dbCos(45) * R);
dbLine(X, Y-R, X+Diff, Y-Diff);
dbLine(X+Diff, Y-Diff, X+R, Y);
dbLine(X+R, Y, X+Diff, Y+Diff);
dbLine(X+Diff, Y+Diff, X, Y+R);
dbLine(X, Y+R, X-Diff, Y+Diff);
dbLine(X-Diff, Y+Diff, X-R, Y);
dbLine(X-R, Y, X-Diff, Y-Diff);
dbLine(X-Diff, Y-Diff, X, Y-R);
}
void DarkGDK()
{
drawOctagon(dbScreenWidth() / 2, dbScreenHeight() / 2, 80);
dbWaitKey();
}