changeset 5:6be2faa7ed6e

rectangles with variable height
author Dennis C. M. <dennis@denniscm.com>
date Tue, 13 Jun 2023 17:23:52 +0100
parents 035d3880da04
children 40a8bdbe2005
files main.c utils.c utils.h
diffstat 3 files changed, 25 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Mon Jun 12 23:10:08 2023 +0100
+++ b/main.c	Tue Jun 13 17:23:52 2023 +0100
@@ -1,4 +1,8 @@
 #include "utils.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <time.h>
 
 
 #define HEIGHT 1080
@@ -30,14 +34,21 @@
 	float x = 1;
 	float rect_width = 5.0;
 	float space = 5.0;
+	
+	// Compute max number of rectangles to fit the windows
 	int max_rects = floor((WIDTH - rect_width) / (rect_width + space)) + x;	
 	
+	// Initialize empty array with same of `max_rects`
+	int *unsorted_array = (int*)malloc(max_rects * sizeof(int));
+
 	int rect_counter = 0;
 	while (rect_counter < max_rects) {
-		glVertex2f(x, 100.0);
-		glVertex2f(x + rect_width, 100.0);
-		glVertex2f(x + rect_width, HEIGHT - 300);
-		glVertex2d(x, HEIGHT - 300);
+		int height = random_int(100, HEIGHT - 100);
+
+		glVertex2f(x, HEIGHT - 100);
+		glVertex2f(x + rect_width, HEIGHT - 100);
+		glVertex2f(x + rect_width, height);
+		glVertex2d(x, height);
 
 		x += rect_width + space;
 		rect_counter++;
@@ -47,7 +58,7 @@
 	
 	char text[256];
 	sprintf(text, "Number of elements: %i", rect_counter);
-    render_text(text, 20.0, HEIGHT - 200);	
+    render_text(text, 20.0, HEIGHT - 50);	
 	glFlush();
 }
 
--- a/utils.c	Mon Jun 12 23:10:08 2023 +0100
+++ b/utils.c	Tue Jun 13 17:23:52 2023 +0100
@@ -8,3 +8,8 @@
 		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
 	}
 }
+
+
+int random_int(int min, int max) {
+	return rand() % ((max - min) + 1) + min;
+}
--- a/utils.h	Mon Jun 12 23:10:08 2023 +0100
+++ b/utils.h	Tue Jun 13 17:23:52 2023 +0100
@@ -1,11 +1,11 @@
 #ifndef UTILS_H
 #define UTILS_H
 
-#include <stdio.h>
+
 #include <GL/glut.h>
-#include <math.h>
+
+void render_text(const char *text, float x, float y);
+int random_int(int min, int max);
 
 
-void render_text(const char *text, float x, float y);
-
 #endif