From a63285a8b51d61877433cc18100fbd584eb91bee Mon Sep 17 00:00:00 2001 From: garrett Date: Thu, 24 Oct 2024 23:45:21 -0400 Subject: [PATCH] Added Graph function --- .gitignore | 5 +++++ graph.py | 26 ++++++++++++++++++++++++++ newMain.py | 22 ++++++++++++++++------ particle.py | 10 +++++++--- 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 graph.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e3174b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +__pycache__/graph.cpython-312.pyc +__pycache__/particle.cpython-312.pyc + +__pycache__/sensor.cpython-312.pyc +__pycache__/slider.cpython-312.pyc diff --git a/graph.py b/graph.py new file mode 100644 index 0000000..0ada16f --- /dev/null +++ b/graph.py @@ -0,0 +1,26 @@ +import pygame + +class Graph: + def __init__(self, ratio, max_time, center, y_scale): + self.ratio = ratio + self.max_time = max_time + self.center = center + self.y_scale = y_scale + self.data = [] + + def draw(self, screen, x, y, time_scale): + rect = pygame.Rect(0, 0, x, y / self.ratio) + pygame.draw.rect(screen, (0,0,0), rect) + self.draw_data(x, y, screen, time_scale) + + def add_data(self, time, value): + self.data.append([time, value]) + + def get_data(self): + return self.data + + def draw_data(self, x, y, screen, time_scale): + offset = 0 + for i in range(len(self.data) - 1, -1, -1): + pygame.draw.circle(screen, (255,0,0), (x - offset ,(y / (2 * self.ratio)) + self.data[i][1] * 10), 1) + offset += time_scale * 10 \ No newline at end of file diff --git a/newMain.py b/newMain.py index 26625a8..f18fbe4 100644 --- a/newMain.py +++ b/newMain.py @@ -5,15 +5,22 @@ import math from sensor import Sensor from particle import Particle from slider import Slider +from graph import Graph SCREEN_WIDTH = 1352 SCREEN_HEIGHT = 878 scale = 1 * pow(10, -6) unit_scale = -3 +time = 0 +time_scale = .1 sensor = Sensor( 50 * pow(10, -6), 30 * pow(10, -6), 20 * pow(10, -6)) +particle = Particle(10 * pow(10, -8), 7 * pow(10, -6), 1, 1) + +graph = Graph(4, 10, 0, 10) + pygame.init() pygame.display.set_caption("CytoSim") @@ -45,23 +52,26 @@ while True: scale_bar_size = abs((x - (x * .1)) - (x - (x * .1)) - (1 * pow(10, unit_scale) / scale)) - print(scale_bar_size) if int(scale_bar_size) < 40: unit_scale += 1 - elif int(scale_bar_size) > 100: + elif int(scale_bar_size) > 500: unit_scale -= 1 scale_bar_end_point = (x - (x * .1)) - (1 * pow(10, unit_scale) / scale) screen.fill((200,100,5)) - sensor.display(x, y, screen, scale) + sensor.display(x, y + (y / graph.ratio), screen, scale) + particle.move(time_scale, scale, sensor.left_limit, sensor.right_limit, screen, y + (y / graph.ratio)) + graph.draw(screen, x, y, time_scale) - pygame.draw.circle(screen, (150,255,10), (x / 2, y /2), 3 * pow(10, -6) / scale) + graph.add_data(time, math.sin(time)) + # pygame.draw.circle(screen, (150,255,10), (x / 2, y /2), 3 * pow(10, -6) / scale) pygame.draw.line(screen, (255,255,255), (x - (x * .1), y - (y * .1)), (scale_bar_end_point, y - (y * .1))) - print(scale) - #print((1 *pow(10, -6)) / scale) + # print((1 *pow(10, -6)) / scale) + + time += time_scale pygame.display.update() diff --git a/particle.py b/particle.py index 8539553..345240a 100644 --- a/particle.py +++ b/particle.py @@ -1,4 +1,5 @@ import math +import pygame class Particle: def __init__(self, speed, size, perm, rest): @@ -7,10 +8,13 @@ class Particle: self.perm = perm self.rest = rest self.volume = (4/3.0) * math.pi * size * size * size + self.distance = 0 - def move(self, time, scale): - distance = (self.speed * time) / scale - return distance + def move(self, time_interval, scale, left_limit, right_limit, screen, height): + self.distance += (self.speed * time_interval) / scale + if self.distance + left_limit + (self.size / (2 * scale)) > right_limit: + self.distance = 0 + pygame.draw.circle(screen, (255,225,255), (left_limit + self.distance, height / 2), self.size / (2 * scale)) def partialVol(self, height): partialVol = (1/3) * math.pi * height * height * ((3 * self.size) - height)