Added Graph function

This commit is contained in:
garrett 2024-10-24 23:45:21 -04:00
parent a47e6f7fed
commit a63285a8b5
4 changed files with 54 additions and 9 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
__pycache__/graph.cpython-312.pyc
__pycache__/particle.cpython-312.pyc
__pycache__/sensor.cpython-312.pyc
__pycache__/slider.cpython-312.pyc

26
graph.py Normal file
View File

@ -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

View File

@ -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)
time += time_scale
pygame.display.update()

View File

@ -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)