Added all previous files
This commit is contained in:
parent
4466ee8a4c
commit
f04fb2189a
59 changed files with 64921 additions and 0 deletions
8
lab10/Makefile
Normal file
8
lab10/Makefile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Makefile for lab10
|
||||
|
||||
fractals: fractals.c
|
||||
gcc fractals.c gfx.o -lX11 -lm -o fractals
|
||||
|
||||
clean:
|
||||
rm fractals
|
||||
|
||||
199
lab10/fractals.c
Normal file
199
lab10/fractals.c
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundamentals of Computing
|
||||
// Assignment: Lab 10
|
||||
// Due: 8 December 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include "gfx.h"
|
||||
|
||||
void sierpinski(int x1, int y1, int x2, int y2, int x3, int y3);
|
||||
void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3);
|
||||
void shrinkingSquares(int x, int y, int sideRadius);
|
||||
void spiralSquares(int centerX, int centerY, int radius, float angle, int sideRadius);
|
||||
void square(int x, int y, int sideRadius);
|
||||
void lace(int x, int y, int radius);
|
||||
void snowflake(int x, int y, int radius, float angle);
|
||||
void tree(int x, int y, int length, float angle, float branchAngle);
|
||||
void fern(int x, int y, int length, float angle, float branchAngle);
|
||||
void spiralSpirals(int x, int y, int radius, double angle);
|
||||
|
||||
int main() {
|
||||
int width = 1000, height = 1000, mrgn = 20;
|
||||
gfx_open(width, height, "Fractals");
|
||||
|
||||
char inpt;
|
||||
|
||||
bool loop = true;
|
||||
while(loop) {
|
||||
inpt = gfx_wait();
|
||||
gfx_clear();
|
||||
|
||||
switch (inpt) {
|
||||
case '1':
|
||||
sierpinski(mrgn, mrgn, width-mrgn, mrgn, width/2, height-mrgn);
|
||||
break;
|
||||
case '2':
|
||||
shrinkingSquares(height/2, height/2, height/4);
|
||||
break;
|
||||
case '3':
|
||||
spiralSquares(width/2, height/2, width/2, 0, height/10);
|
||||
break;
|
||||
case '4':
|
||||
lace(width/2, height/2, height*1/3);
|
||||
break;
|
||||
case '5':
|
||||
snowflake(width/2, height/2, height/3, M_PI/2);
|
||||
break;
|
||||
case '6':
|
||||
tree(width/2, height-20, height/3.2, -M_PI/2, M_PI/6);
|
||||
break;
|
||||
case '7':
|
||||
fern(width/2, height-20, height/1.5, -M_PI/2, M_PI/4);
|
||||
break;
|
||||
case '8':
|
||||
spiralSpirals(width/2, height/2, width, 0);
|
||||
break;
|
||||
case 'q':
|
||||
loop = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sierpinski(int x1, int y1, int x2, int y2, int x3, int y3) {
|
||||
// Base case
|
||||
if (abs(x2-x1) < 5) return;
|
||||
|
||||
// Draw a triangle
|
||||
drawTriangle(x1, y1, x2, y2, x3, y3);
|
||||
|
||||
// Recursive calls
|
||||
sierpinski(x1, y1, (x1+x2)/2, (y1+y2)/2, (x1+x3)/2, (y1+y3)/2);
|
||||
sierpinski((x1+x2)/2, (y1+y2)/2, x2, y2, (x2+x3)/2, (y2+y3)/2);
|
||||
sierpinski((x1+x3)/2, (y1+y3)/2, (x2+x3)/2, (y2+y3)/2, x3, y3);
|
||||
}
|
||||
|
||||
void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
|
||||
gfx_line(x1, y1, x2, y2);
|
||||
gfx_line(x2, y2, x3, y3);
|
||||
gfx_line(x3, y3, x1, y1);
|
||||
}
|
||||
|
||||
void shrinkingSquares(int x, int y, int sideRadius) {
|
||||
// Base case
|
||||
if (sideRadius < 2) return;
|
||||
|
||||
// Draw a square
|
||||
square(x, y, sideRadius);
|
||||
|
||||
// Recursive calls
|
||||
shrinkingSquares(x-sideRadius, y-sideRadius, sideRadius/2.1);
|
||||
shrinkingSquares(x+sideRadius, y-sideRadius, sideRadius/2.1);
|
||||
shrinkingSquares(x+sideRadius, y+sideRadius, sideRadius/2.1);
|
||||
shrinkingSquares(x-sideRadius, y+sideRadius, sideRadius/2.1);
|
||||
}
|
||||
|
||||
void spiralSquares(int centerX, int centerY, int radius, float angle, int sideRadius) {
|
||||
// Base case
|
||||
if (sideRadius < 2) return;
|
||||
|
||||
// Draw a square
|
||||
square(centerX + radius*cos(angle), centerY + radius*sin(angle), sideRadius);
|
||||
|
||||
// Recursive call
|
||||
spiralSquares(centerX, centerY, radius*0.9, angle+M_PI*0.2, sideRadius*0.9);
|
||||
}
|
||||
|
||||
void square(int x, int y, int sideRadius) {
|
||||
gfx_line(x-sideRadius, y-sideRadius, x+sideRadius, y-sideRadius);
|
||||
gfx_line(x+sideRadius, y-sideRadius, x+sideRadius, y+sideRadius);
|
||||
gfx_line(x+sideRadius, y+sideRadius, x-sideRadius, y+sideRadius);
|
||||
gfx_line(x-sideRadius, y+sideRadius, x-sideRadius, y-sideRadius);
|
||||
}
|
||||
|
||||
void lace(int x, int y, int radius) {
|
||||
// Base case
|
||||
if (radius < 2) return;
|
||||
|
||||
// Draw a circle
|
||||
gfx_circle(x, y, radius);
|
||||
|
||||
// Recursive calls
|
||||
for (float i = 0; i < 2; i += 1.0f/3.0f) {
|
||||
lace(x + radius*cos(M_PI*i), y + radius*sin(M_PI*i), radius/3);
|
||||
}
|
||||
}
|
||||
|
||||
void snowflake(int x, int y, int radius, float angle) {
|
||||
// Base case
|
||||
if (radius < 2) return;
|
||||
|
||||
// Recursive calls with drawing
|
||||
int numLines = 5;
|
||||
float angleStep = 2 * M_PI / numLines;
|
||||
for (int i = 0; i < numLines; i++) {
|
||||
float newAngle = angle + i * angleStep;
|
||||
int newX = x + radius * cos(newAngle);
|
||||
int newY = y + radius * sin(newAngle);
|
||||
gfx_line(x, y, newX, newY);
|
||||
snowflake(newX, newY, radius / 3, newAngle);
|
||||
}
|
||||
}
|
||||
|
||||
void tree(int x, int y, int length, float angle, float branchAngle) {
|
||||
// Base case
|
||||
if (length < 5) return;
|
||||
|
||||
// Draw a line
|
||||
int newX = x + length * cos(angle);
|
||||
int newY = y + length * sin(angle);
|
||||
gfx_line(x, y, newX, newY);
|
||||
|
||||
// Recursive calls
|
||||
tree(newX, newY, length * 0.7, angle - branchAngle, branchAngle);
|
||||
tree(newX, newY, length * 0.7, angle + branchAngle, branchAngle);
|
||||
}
|
||||
|
||||
void fern(int x, int y, int length, float angle, float branchAngle) {
|
||||
// Base case
|
||||
if (length < 5) return;
|
||||
|
||||
// Draw a line
|
||||
int endX = x + length * cos(angle);
|
||||
int endY = y + length * sin(angle);
|
||||
gfx_line(x, y, endX, endY);
|
||||
|
||||
// Calculate new points along the line
|
||||
int numPoints = 4;
|
||||
int pointsX[numPoints];
|
||||
int pointsY[numPoints];
|
||||
for (int i = 0; i < numPoints; i++) {
|
||||
pointsX[i] = x + (i * length / numPoints + length / numPoints) * cos(angle);
|
||||
pointsY[i] = y + (i * length / numPoints + length / numPoints) * sin(angle);
|
||||
}
|
||||
|
||||
// Recursive calls
|
||||
for (int i = 0; i < numPoints; i++) {
|
||||
fern(pointsX[i], pointsY[i], length / 3, angle - branchAngle, branchAngle);
|
||||
fern(pointsX[i], pointsY[i], length / 3, angle + branchAngle, branchAngle);
|
||||
}
|
||||
}
|
||||
|
||||
void spiralSpirals(int x, int y, int radius, double angle) {
|
||||
// Base case
|
||||
if (radius < 2) return;
|
||||
|
||||
// Draw point with new coordinates
|
||||
int newX = x + radius * cos(angle);
|
||||
int newY = y + radius * sin(angle);
|
||||
gfx_point(newX, newY);
|
||||
|
||||
// Recursive cases
|
||||
spiralSpirals(x, y, radius*0.9, angle + M_PI*0.2);
|
||||
spiralSpirals(newX, newY, radius*0.35, angle);
|
||||
}
|
||||
51
lab10/gfx.h
Normal file
51
lab10/gfx.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// A simple graphics library for CSE 20311
|
||||
// Last edit: Nov 15, 2024
|
||||
|
||||
#ifndef GFX_H
|
||||
#define GFX_H
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
// Open a new graphics window.
|
||||
void gfx_open( int width, int height, const char *title );
|
||||
|
||||
// Flush all previous output to the window.
|
||||
void gfx_flush();
|
||||
|
||||
// Change the current drawing color.
|
||||
void gfx_color( int red, int green, int blue );
|
||||
|
||||
// Clear the graphics window to the background color.
|
||||
void gfx_clear();
|
||||
|
||||
// Change the current background color.
|
||||
void gfx_clear_color( int red, int green, int blue );
|
||||
|
||||
// Check to see if an event is waiting.
|
||||
int gfx_event_waiting();
|
||||
|
||||
// Wait for the user to press a key or mouse button.
|
||||
char gfx_wait();
|
||||
|
||||
// Return the X and Y coordinates of the last event.
|
||||
int gfx_xpos();
|
||||
int gfx_ypos();
|
||||
|
||||
// Return the X and Y dimensions of the screen (monitor).
|
||||
int gfx_xsize();
|
||||
int gfx_ysize();
|
||||
|
||||
// Draw a point at (x,y)
|
||||
void gfx_point( int x, int y );
|
||||
|
||||
// Draw a line from (x1,y1) to (x2,y2)
|
||||
void gfx_line( int x1, int y1, int x2, int y2 );
|
||||
|
||||
// Draw a circle centered at (xc,yc) with radius r
|
||||
void gfx_circle( int xc, int yc, int r );
|
||||
|
||||
// Display a string at (x,y)
|
||||
void gfx_text( int x, int y , const char *text );
|
||||
|
||||
#endif
|
||||
|
||||
BIN
lab10/gfx.o
Normal file
BIN
lab10/gfx.o
Normal file
Binary file not shown.
Loading…
Reference in a new issue