-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e131fe0
commit 427ba69
Showing
5 changed files
with
464 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
import numpy as np | ||
from mpl_toolkits.mplot3d import Axes3D | ||
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection | ||
import matplotlib.pyplot as plt | ||
|
||
df = pd.read_csv("uniquesolutions.csv",header=None,sep='\t') | ||
myArray = df.values | ||
|
||
points = solutionsarray | ||
|
||
def connectpoints(x,y,p1,p2): | ||
x1, x2 = x[p1], x[p2] | ||
y1, y2 = y[p1], y[p2] | ||
plt.plot([x1,x2],[y1,y2],'k-') | ||
|
||
# cube[0][0][0] = 1 | ||
# cube[0][0][1] = 2 | ||
# cube[0][1][0] = 3 | ||
# cube[0][1][1] = 4 | ||
# cube[1][0][0] = 5 | ||
# cube[1][0][1] = 6 | ||
# cube[1][1][0] = 7 | ||
# cube[1][1][1] = 8 | ||
|
||
for i in range(): | ||
connectpoints(cube[i][i][i],cube[],points[i],points[i+1]) # Confused! | ||
|
||
|
||
|
||
ax = fig.add_subplot(111, projection='3d') | ||
# plot sides | ||
|
||
ax.add_collection3d(Poly3DCollection(verts, | ||
facecolors='cyan', linewidths=1, edgecolors='r', alpha=.25)) | ||
|
||
ax.set_xlabel('X') | ||
ax.set_ylabel('Y') | ||
ax.set_zlabel('Z') | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Import libraries | ||
import matplotlib.pyplot as plt | ||
from mpl_toolkits.mplot3d import Axes3D | ||
import numpy as np | ||
|
||
%matplotib inline | ||
%pylab inline | ||
|
||
# Create axis | ||
axes = [5, 5, 5] | ||
|
||
# Create Data | ||
data = np.ones(axes, dtype=bool) | ||
|
||
# Control Transparency | ||
alpha = 0.9 | ||
|
||
# Control colour | ||
colors = np.empty(axes + [4], dtype=np.float32) | ||
|
||
colors[:] = [1, 0, 0, alpha] # red | ||
|
||
# Plot figure | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111, projection='3d') | ||
|
||
# Voxels is used to customizations of the | ||
# sizes, positions and colors. | ||
ax.voxels(data, facecolors=colors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import sys, math, pygame | ||
from operator import itemgetter | ||
|
||
class Point3D: | ||
def __init__(self, x = 0, y = 0, z = 0): | ||
self.x, self.y, self.z = float(x), float(y), float(z) | ||
|
||
def rotateX(self, angle): | ||
""" Rotates the point around the X axis by the given angle in degrees. """ | ||
rad = angle * math.pi / 180 | ||
cosa = math.cos(rad) | ||
sina = math.sin(rad) | ||
y = self.y * cosa - self.z * sina | ||
z = self.y * sina + self.y * cosa | ||
return Point3D(self.x, y, z) | ||
|
||
def rotateY(self, angle): | ||
""" Rotates the point around the Y axis by the given angle in degrees. """ | ||
rad = angle * math.pi / 180 | ||
cosa = math.cos(rad) | ||
sina = math.sin(rad) | ||
z = self.z * cosa - self.x * sina | ||
x = self.z * sina + self.x * cosa | ||
return Point3D(x, self.y, z) | ||
|
||
def rotateZ(self, angle): | ||
""" Rotates the point around the Z axis by the given angle in degrees. """ | ||
rad = angle * math.pi / 180 | ||
cosa = math.cos(rad) | ||
sina = math.sin(rad) | ||
x = self.x * cosa - self.y * sina | ||
y = self.x * sina + self.y * cosa | ||
return Point3D(x, y, self.z) | ||
|
||
def project(self, win_width, win_height, fov, viewer_distance): | ||
""" Transforms this 3D point to 2D using a perspective projection. """ | ||
factor = fov / (viewer_distance + self.z) | ||
x = self.x * factor + win_width / 2 | ||
y = -self.y * factor + win_height / 2 | ||
return Point3D(x, y, self.z) | ||
|
||
class Simulation: | ||
def __init__(self, win_width = 640, win_height = 480): | ||
pygame.init() | ||
|
||
self.screen = pygame.display.set_mode((win_width, win_height)) | ||
pygame.display.set_caption("Rotating Cube") | ||
|
||
self.clock = pygame.time.Clock() | ||
|
||
self.vertices = [ | ||
Point3D(-1,1,-1), | ||
Point3D(1,1,-1), | ||
Point3D(1,-1,-1), | ||
Point3D(-1,-1,-1), | ||
Point3D(-1,1,1), | ||
Point3D(1,1,1), | ||
Point3D(1,-1,1), | ||
Point3D(-1,-1,1) | ||
] | ||
|
||
# Define the vertices that compose each of the 6 faces. These numbers are | ||
# indices to the vertices list defined above. | ||
self.faces = [(0,1,2,3),(1,5,6,2),(5,4,7,6),(4,0,3,7),(0,4,5,1),(3,2,6,7)] | ||
|
||
# Define colors for each face | ||
self.colors = [(255,0,255),(255,0,0),(0,255,0),(0,0,255),(0,255,255),(255,255,0)] | ||
|
||
self.angle = 0 | ||
|
||
def run(self): | ||
""" Main Loop """ | ||
while 1: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
sys.exit() | ||
|
||
self.clock.tick(50) | ||
self.screen.fill((150,150,0)) | ||
|
||
# It will hold transformed vertices. | ||
t = [] | ||
|
||
for v in self.vertices: | ||
# Rotate the point around X axis, then around Y axis, and finally around Z axis. | ||
r = v.rotateX(self.angle).rotateY(self.angle).rotateZ(self.angle) | ||
# Transform the point from 3D to 2D | ||
p = r.project(self.screen.get_width(), self.screen.get_height(), 256, 4) | ||
# Put the point in the list of transformed vertices | ||
t.append(p) | ||
|
||
# Calculate the average Z values of each face. | ||
avg_z = [] | ||
i = 0 | ||
for f in self.faces: | ||
z = (t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z) / 4.0 | ||
avg_z.append([i,z]) | ||
i = i + 1 | ||
|
||
# Draw the faces using the Painter's algorithm: | ||
# Distant faces are drawn before the closer ones. | ||
for tmp in sorted(avg_z,key=itemgetter(1),reverse=True): | ||
face_index = tmp[0] | ||
f = self.faces[face_index] | ||
pointlist = [(t[f[0]].x, t[f[0]].y), (t[f[1]].x, t[f[1]].y), | ||
(t[f[1]].x, t[f[1]].y), (t[f[2]].x, t[f[2]].y), | ||
(t[f[2]].x, t[f[2]].y), (t[f[3]].x, t[f[3]].y), | ||
(t[f[3]].x, t[f[3]].y), (t[f[0]].x, t[f[0]].y)] | ||
pygame.draw.polygon(self.screen,self.colors[face_index],pointlist) | ||
|
||
self.angle += 1 | ||
|
||
pygame.display.flip() | ||
|
||
x = Simulation() | ||
x.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import sys | ||
import math | ||
import pygame | ||
|
||
from pygame.math import Vector3 | ||
from enum import Enum | ||
|
||
|
||
class Color(Enum): | ||
BLACK = (0, 0, 0) | ||
SILVER = (192,192,192) | ||
|
||
|
||
class Cube(): | ||
|
||
def __init__(self, vectors, screen_width, screen_height, initial_angle=25): | ||
self._vectors = vectors | ||
self._angle = initial_angle | ||
self._screen_width = screen_width | ||
self._screen_height = screen_height | ||
|
||
# Define the vectors that compose each of the 6 faces | ||
self._faces = [(0,1,2,3), | ||
(1,5,6,2), | ||
(5,4,7,6), | ||
(4,0,3,7), | ||
(0,4,5,1), | ||
(3,2,6,7)] | ||
|
||
self._setup_initial_positions(initial_angle) | ||
|
||
def _setup_initial_positions(self, angle): | ||
tmp = [] | ||
for vector in self._vectors: | ||
rotated_vector = vector.rotate_x(angle).rotate_y(angle)#.rotateZ(self.angle) | ||
tmp.append(rotated_vector) | ||
|
||
self._vectors = tmp | ||
|
||
def transform_vectors(self, new_angle): | ||
# It will hold transformed vectors. | ||
transformed_vectors = [] | ||
|
||
for vector in self._vectors: | ||
# Rotate the point around X axis, then around Y axis, and finally around Z axis. | ||
mod_vector = vector.rotate_y(new_angle) | ||
# Transform the point from 3D to 2D | ||
mod_vector = self._project(mod_vector, self._screen_width, self._screen_height, 256, 4) | ||
# Put the point in the list of transformed vectors | ||
transformed_vectors.append(mod_vector) | ||
|
||
return transformed_vectors | ||
|
||
def _project(self, vector, win_width, win_height, fov, viewer_distance): | ||
factor = fov / (viewer_distance + vector.z) | ||
x = vector.x * factor + win_width / 2 | ||
y = -vector.y * factor + win_height / 2 | ||
return Vector3(x, y, vector.z) | ||
|
||
def calculate_average_z(self, vectors): | ||
avg_z = [] | ||
for i, face in enumerate(self._faces): | ||
# for each point of a face calculate the average z value | ||
z = (vectors[face[0]].z + | ||
vectors[face[1]].z + | ||
vectors[face[2]].z + | ||
vectors[face[3]].z) / 4.0 | ||
avg_z.append([i, z]) | ||
|
||
return avg_z | ||
|
||
def get_face(self, index): | ||
return self._faces[index] | ||
|
||
def create_polygon(self, face, transformed_vectors): | ||
return [(transformed_vectors[face[0]].x, transformed_vectors[face[0]].y), | ||
(transformed_vectors[face[1]].x, transformed_vectors[face[1]].y), | ||
(transformed_vectors[face[2]].x, transformed_vectors[face[2]].y), | ||
(transformed_vectors[face[3]].x, transformed_vectors[face[3]].y), | ||
(transformed_vectors[face[0]].x, transformed_vectors[face[0]].y)] | ||
|
||
|
||
class Simulation: | ||
def __init__(self, win_width=640, win_height=480): | ||
pygame.init() | ||
|
||
self.screen = pygame.display.set_mode((win_width, win_height)) | ||
|
||
self.clock = pygame.time.Clock() | ||
|
||
cube = Cube([ | ||
Vector3(0, 0.5, -0.5), | ||
Vector3(0.5, 0.5, -0.5), | ||
Vector3(0.5, 0, -0.5), | ||
Vector3(0, 0, -0.5), | ||
Vector3(0, 0.5, 0), | ||
Vector3(0.5, 0.5, 0), | ||
Vector3(0.5, 0, 0), | ||
Vector3(0, 0, 0) | ||
], win_width, win_height) | ||
|
||
cube2 = Cube([ | ||
Vector3(0.5, 0.5, -0.5), | ||
Vector3(1, 0.5, -0.5), | ||
Vector3(1, 0, -0.5), | ||
Vector3(0.5, 0, -0.5), | ||
Vector3(0.5, 0.5, 0), | ||
Vector3(1, 0.5, 0), | ||
Vector3(1, 0, 0), | ||
Vector3(0.5, 0, 0) | ||
], win_width, win_height) | ||
|
||
self._angle = 30 | ||
|
||
self._cubes = [cube, cube2] | ||
|
||
def run(self): | ||
while True: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
sys.exit() | ||
|
||
self.clock.tick(50) | ||
self.screen.fill(Color.BLACK.value) | ||
|
||
for cube in self._cubes: | ||
transformed_vectors = cube.transform_vectors(self._angle) | ||
avg_z = cube.calculate_average_z(transformed_vectors) | ||
|
||
# Draw the faces using the Painter's algorithm: | ||
# Distant faces are drawn before the closer ones. | ||
for avg_z in sorted(avg_z, key=lambda x: x[1], reverse=True): | ||
face_index = avg_z[0] | ||
face = cube._faces[face_index] | ||
pointlist = cube.create_polygon(face, transformed_vectors) | ||
|
||
pygame.draw.polygon(self.screen, Color.SILVER.value,pointlist) | ||
pygame.draw.polygon(self.screen, Color.BLACK.value, pointlist, 3) | ||
# break | ||
|
||
self._angle += 1 | ||
|
||
pygame.display.flip() | ||
|
||
if __name__ == "__main__": | ||
Simulation().run() |
Oops, something went wrong.