summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-02 11:16:13 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-02 11:16:13 -0400
commit66559ba8c23e5d1591336a703785a7bc42e98f2d (patch)
tree13b61fe6b175107c3a1f30ebc57a8388ecfc177d
downloadballs-66559ba8c23e5d1591336a703785a7bc42e98f2d.zip
drawing
-rw-r--r--.gitignore3
-rw-r--r--Makefile10
-rw-r--r--balls.cpp104
3 files changed, 117 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1a94e9b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.o
+balls
+*.pdf
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..be1b32b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,10 @@
+CC = g++
+CFLAGS = -Wall -pedantic
+LDFLAGS = -ltbb -lglut -lGLU -lGL
+
+balls: balls.o
+ ${CC} -o $@ $^ ${LDFLAGS}
+ @echo done
+
+%.o: %.cpp
+ ${CC} -c ${CFLAGS} $< \ No newline at end of file
diff --git a/balls.cpp b/balls.cpp
new file mode 100644
index 0000000..555b192
--- /dev/null
+++ b/balls.cpp
@@ -0,0 +1,104 @@
+#include <stdlib.h>
+#include <GL/glut.h>
+#include <oneapi/tbb.h>
+
+using namespace std;
+
+enum window { WIDTH = 800, HEIGHT = 600 };
+enum keys { KEY_QUIT = 'q' };
+enum { CIRCLE_SEGS = 32 };
+
+typedef struct {
+ double x, y;
+} Point;
+
+typedef struct {
+ Point min, max;
+} Rectangle;
+
+void keyboard(unsigned char key, int x, int y);
+void display(void);
+void drawBg(void);
+void drawCircle(double radius, Point p);
+void reshape(int w, int h);
+
+const static Rectangle bounds = {{-1.5, -1.0}, {1.5, 1.0}};
+
+int
+main(int argc, char *argv[]) {
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
+ glutInitWindowSize(WIDTH, HEIGHT);
+ glutCreateWindow("Balls");
+
+ glutKeyboardFunc(keyboard);
+ glutDisplayFunc(display);
+ glutReshapeFunc(reshape);
+
+ glClearColor(1.0, 1.0, 1.0, 1.0);
+
+ glutMainLoop();
+
+ return 1;
+}
+
+void
+keyboard(unsigned char key, int x, int y) {
+ if (key == KEY_QUIT)
+ glutDestroyWindow(glutGetWindow());
+}
+
+void
+display(void) {
+ static Point p = {0.35, 0.35};
+
+ glClearColor(0, 0, 0, 1);
+ glClear(GL_COLOR_BUFFER_BIT);
+ drawBg();
+ drawCircle(0.25, p);
+
+ glutSwapBuffers();
+}
+
+void
+drawBg(void) {
+ glColor3f(1, 1, 1);
+ glBegin(GL_QUADS);
+ glVertex2f(bounds.min.x, bounds.min.y);
+ glVertex2f(bounds.max.x, bounds.min.y);
+ glVertex2f(bounds.max.x, bounds.max.y);
+ glVertex2f(bounds.min.x, bounds.max.y);
+ glEnd();
+}
+
+void
+drawCircle(double radius, Point p) {
+ int i;
+ double theta, x, y;
+
+ glColor3f(0.1, 0.6, 0.8);
+ glBegin(GL_TRIANGLE_FAN);
+ glVertex2f(p.x, p.y);
+ for (i = 0; i <= CIRCLE_SEGS; i++) {
+ theta = 2.0 * M_PI * i / CIRCLE_SEGS;
+ x = radius * cosf(theta);
+ y = radius * sinf(theta);
+ glVertex2f(x+p.x, y+p.y);
+ }
+ glEnd();
+}
+
+void
+reshape(int w, int h) {
+ double ratio;
+
+ glViewport(0, 0, w, h);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ ratio = (double) w / (double) h;
+ if (ratio >= 1.0)
+ glOrtho(-ratio, ratio, -1, 1, -1, 1);
+ else
+ glOrtho(-1, 1, -1.0/ratio, 1.0/ratio, -1, 1);
+ glMatrixMode(GL_MODELVIEW);
+}