Computergraphik_Hopp/Code/template_koordinatensystemUndPunkte.c

168 lines
5.3 KiB
C
Raw Normal View History

2018-10-24 15:03:05 +02:00
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
2018-10-31 14:45:06 +01:00
#include <string.h>
2018-10-24 15:03:05 +02:00
#define PI 3.14159265358979323846
2018-10-24 15:45:27 +02:00
#define PIf 3.14159265358979323846f
2018-10-24 15:03:05 +02:00
2018-10-24 15:45:27 +02:00
static int width = 800;
static int height = 600;
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
void init(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glViewport(0, 0, width, height);
glutCreateWindow("Programmieruebung Kreis-Plotter");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2018-10-24 15:03:05 +02:00
}
2018-10-31 14:45:06 +01:00
void drawCoordinateSystem(float* origin, int xLength, int yLength, int numTicksX, int numTicksY,
float minX, float maxX, float minY, float maxY)
{
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
float xTickDistance = (float)xLength / (float)numTicksX;
float yTickDistance = (float)yLength / (float)numTicksY;
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
char label[10];
2018-10-24 15:03:05 +02:00
2018-10-24 15:45:27 +02:00
double resX = (maxX - minX) / xLength;
double resY = (maxY - minY) / yLength;
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
glColor3f(0.0, 0.0, 0.0);
/* drawing the coordinate system */
glBegin(GL_LINES);
// x axis
glVertex2f(origin[0], origin[1]);
glVertex2f(origin[0] + xLength, origin[1]);
// y axis
glVertex2f(origin[0], origin[1]);
glVertex2f(origin[0], origin[1] + yLength);
// arrow head at x axis
glVertex2f(origin[0] + xLength, origin[1]);
glVertex2f(origin[0] + xLength - 15, origin[1] - 10);
glVertex2f(origin[0] + xLength, origin[1]);
glVertex2f(origin[0] + xLength - 15, origin[1] + 10);
// arrow head at y axis
glVertex2f(origin[0], origin[1] + yLength);
glVertex2f(origin[0] - 10, origin[1] + yLength - 15);
glVertex2f(origin[0], origin[1] + yLength);
glVertex2f(origin[0] + 10, origin[1] + yLength - 15);
glEnd();
// drawing ticks at x axis
2018-10-24 15:45:27 +02:00
for (int j = 0; j < xLength; j = j + xTickDistance) {
2018-10-31 14:45:06 +01:00
glBegin(GL_LINES);
glVertex2f(origin[0] + j, origin[1] - 5);
glVertex2f(origin[0] + j, origin[1] + 5);
glEnd();
sprintf(label, "%5.2f", minX + (j * resX));
glRasterPos2i((int)round(origin[0] + j - (8 * 5)), (int)round(origin[1] - 13 - 10));
2018-10-24 15:45:27 +02:00
for (int k = 0; k < 5; k++) {
2018-10-31 14:45:06 +01:00
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
}
}
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
// drawing ticks at y axis
2018-10-24 15:45:27 +02:00
for (int j = 0; j < yLength; j = j + yTickDistance) {
2018-10-31 14:45:06 +01:00
glBegin(GL_LINES);
glVertex2f(origin[0] - 5, origin[1] + j);
glVertex2f(origin[0] + 5, origin[1] + j);
glEnd();
sprintf(label, "%5.2f", minY + (j * resY));
glRasterPos2i((int)round(origin[0] - (8 * 5) - 10), (int)round(origin[1] + j - 13 / 2));
2018-10-24 15:45:27 +02:00
for (int k = 0; k < 5; k++) {
2018-10-31 14:45:06 +01:00
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
}
}
2018-10-24 15:03:05 +02:00
}
void drawCircle(float origin[2], float center[2], float radius, GLint numPoints)
{
2018-10-31 14:45:06 +01:00
//////////////////////////////////////////////////////////////////////////////////////////////
// - calculate numPoints points on the surface of the circle with given center and radius
// hint: see circle equation in parametric form
// - position points in the coordinate system starting at given origin
// - draw the points by OpenGL commands
2018-10-24 15:03:05 +02:00
2018-10-24 15:45:27 +02:00
glBegin(GL_POINTS);
2018-10-31 14:45:06 +01:00
for (float i = 0.0f; i < 2 * PIf; i += (2 * PIf) / numPoints) {
2018-10-24 15:45:27 +02:00
float x = origin[0] + center[0] + cosf(i) + radius * cosf(i);
float y = origin[1] + center[1] + sinf(i) + radius * sinf(i);
glVertex2f(x, y);
}
glEnd();
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
//////////////////////////////////////////////////////////////////////////////////////////////
2018-10-24 15:03:05 +02:00
}
void display(void)
{
2018-10-31 14:45:06 +01:00
int n;
int xTickDistance = 50;
int yTickDistance = 50;
int numPoints = 30;
float radius[] = { 50, 100, 150 };
float center[] = { 300, 200 };
float origin[] = { 100, 100 };
GLfloat c[] = { 0.0, 0.0, 0.0 };
int xLength = 600;
int yLength = 400;
float size = 2;
/* settings for the coordinate system */
int numTicksX = 6;
int numTicksY = 4;
float minX = 0;
float maxX = 600;
float minY = 0;
float maxY = 400;
/* clearing the background color */
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
drawCoordinateSystem(origin, xLength, yLength, numTicksX, numTicksY, minX, maxX, minY, maxY);
for (n = 0; n < 3; n++) {
///////////////////////////////////////////////////////////////////////////////
// - set the point size different for each loop iteration
// - set the color different for each loop iteration
2018-10-24 15:03:05 +02:00
2018-10-24 15:45:27 +02:00
if (n == 0) {
glColor3f(1.0f, 0.0f, 0.0f);
glPointSize(2.0f);
} else if (n == 1) {
glColor3f(0.0f, 1.0f, 0.0f);
glPointSize(4.0f);
} else if (n == 2) {
glColor3f(0.0f, 0.0f, 1.0f);
glPointSize(6.0f);
}
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
//////////////////////////////////////////////////////////////////////////////
drawCircle(origin, center, radius[n], numPoints);
}
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
glFlush();
2018-10-24 15:03:05 +02:00
}
int main(int argc, char** argv)
{
2018-10-31 14:45:06 +01:00
init(argc, argv);
2018-10-24 15:03:05 +02:00
2018-10-31 14:45:06 +01:00
glutDisplayFunc(display);
glutMainLoop();
return 0;
2018-10-24 15:03:05 +02:00
}