Skip to content

Commit

Permalink
Merge pull request #2 from rahul-gohil/extension
Browse files Browse the repository at this point in the history
Extension
  • Loading branch information
rahul-gohil committed May 4, 2020
2 parents 9cb7fa1 + 13330a0 commit c61032b
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 154 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# GFKT
GFKT is a visualizer for how we can generate [Kepler Triangles](https://en.wikipedia.org/wiki/Kepler_triangle) and through their special arrangement we can obtain these [Logarithmic Spirals](https://en.wikipedia.org/wiki/Logarithmic_spiral).
GFKT is a visualizer for generating [Kepler Triangles](https://en.wikipedia.org/wiki/Kepler_triangle) and through their special arrangement obatining [Logarithmic Spirals](https://en.wikipedia.org/wiki/Logarithmic_spiral).

[![DOI](https://zenodo.org/badge/256438420.svg)](https://doi.org/10.5281/zenodo.3779050)
## Getting Started
Expand Down Expand Up @@ -27,9 +27,25 @@ Test run with `-noplot`
~/GFKT/src$ python3 main.py -noplot
Enter x1, y1, f(0) & f(1) 0 0 3 4
Enter the number of points 150
Converged at Satisfaction Factor 0 and Angle Factor 90.0 in Iteration 37
Converged to Similarity between Triangles in Iteration 40
Converged at "a" 2.7035313755677604 and "k" 0.15317448126501637 in Iteration 141
{
"lines": {
"comment": "Converged at expected values",
"Angle Factor": 90.0,
"Satisfaction Factor": 0,
"iteration": 35
},
"triangles": {
"comment": "Converged to similarity between triangles",
"factor": 1.272019649514069,
"iteration": 36
},
"logarithmicSpiral": {
"comment": "Converged at expected values",
"a": 2.7035313754910306,
"k": 0.1531744812651672,
"iteration": 118
}
}
```
If you want a plot, the program will ask you for the number of points to plot.
### Poltting with `matplotlib`
Expand All @@ -42,7 +58,7 @@ You can use your mouse wheel to directly zoom in/out of the plot.
### Plotting with `manim`
To get a `480p` video for the plot, just run the file as is, but to get a `1080p` video remove the `-pl` option in `main.py` file at the bottom.

It will generate a video similar to this gif.
It will generate a video similar to this gif.(Recommended to plot 10 ~ 20 points)


![Manim-plot](/img/Shapes-Manim.gif)
Expand Down
15 changes: 11 additions & 4 deletions src/csvUtils/functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import csv

'''Listifies points -> [P.x, P.y]'''
listifyP = lambda points : list(map(
lambda point : [point.x, point.y],
points
))

'''Listifies lines -> [P1.attr, P2.attr]'''
listifyL = lambda lines : list(map(
lambda line : listifyP([
line.point1,
Expand All @@ -18,16 +20,21 @@ def write(fileName, fields, rows):
csvWriter = csv.writer(_file)
csvWriter.writerow(fields)
csvWriter.writerows(rows)

def writePoints(points, fields):
rows = listifyP(points)
write('Points.csv', fields, rows)

def writeLines(lines, fields):
rows = listifyL(lines)
write('Lines.csv', fields, rows)

def writeTriangles(triangles, fields):
'''Listifies triangles -> [
line1.attr,
line2.attr,
line3.attr
]'''
rows = list(map(
lambda triangle : listifyL([
triangle.line1,
Expand All @@ -37,7 +44,7 @@ def writeTriangles(triangles, fields):
triangles
))
write('Triangles.csv', fields, rows)

def writeLogSpiral(spiral, fields):
rows = [[
spiral.a,
Expand Down
1 change: 1 addition & 0 deletions src/csvUtils/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from csvUtils.functions import *

def writeToCsv(points, lines, triangles, spiral):
'''Creates src/data to store csv files and writes'''
try:
mkdir(path.join(
path.abspath('.'),
Expand Down
42 changes: 32 additions & 10 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import solver
import json
import sys
import os

from matplotlibUtils.plotter import plot
from csvUtils.writer import writeToCsv
from csvUtils.writer import writeToCsv
from mpl.plotter import plot

from shapes.logSpiral import *
from shapes.triangle import *
from shapes.point import *
from shapes.line import *

def prettify(d):
'''Prettifies dictionary'''
return json.dumps(
d,
sort_keys = False,
indent = 4,
separators = (
',',
': '
)
)

engine = 'mpl'
if len(sys.argv) > 1:
if sys.argv[1] == '-manim':
Expand All @@ -18,22 +31,27 @@
engine = None
else:
raise ValueError('Argument should be -manim or -noplot')

f = solver.f

x1, y1, solver.f0, solver.f1 = map(
float,
input("Enter x1, y1, f(0) & f(1)\t").split()
)
n = int(input("Enter the number of points\t"))
if engine is not None:
n1 = int(input("Enter the number of points to plot\t"))

assert 0 <= f(0), "f(0) has to be positive"
assert 0 <= f(1), "f(1) has to be positive"
assert x1 < f(0), "f(0) has to be greater than x1"
assert y1 < f(1), "f(1) has to be greater than y1"
assert f(0) < f(1), "f(1) has to be greater than f(0)"

n = int(input("Enter the number of points\t"))

assert n <= 1400, "Too Large for computing f(n)"

if engine is not None:
n1 = int(input("Enter the number of points to plot\t"))

I0 = Point(x1, y1)
I1 = Point(f(0) + x1, y1)
points.append(Point(f(0) + x1, f(1) + y1))
Expand All @@ -51,9 +69,13 @@
triangles.append(T0)
makeTriangles(n + 1)

limitizeLine()
limitizeTriangle()
limitizeLogSpiral(I0, n)
print(prettify(
dict([
limitizeLine(),
limitizeTriangle(),
limitizeLogSpiral(I0, n)
])
))

I0.x, I0.y = 0, 0
I1.x, I1.y = f(0), 0
Expand All @@ -70,5 +92,5 @@
Spiral()
)
os.system(
f'manim ./manimUtils/allAnim.py {n1} Shapes -pl'
f'manim ./manim/allAnim.py {n1} Shapes -pl'
)
26 changes: 13 additions & 13 deletions src/manimUtils/allAnim.py → src/manim/allAnim.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
X, Y = readPoints(n)
a, k, p = readSpiral()

_min = min(min(X), min(Y))
_max = max(max(X), max(Y))
_min = min(*X, *Y)
_max = max(*X, *Y)

class Shapes(GraphScene):

CONFIG = {
Expand All @@ -24,8 +24,9 @@ class Shapes(GraphScene):
"function_color": WHITE,
"axes_color": BLUE,
}

def logSpiralSequence(self, create, fade):
'''Create and Fade sequence for logSpiral'''
self.play(
create,
run_time = 6
Expand All @@ -34,17 +35,17 @@ def logSpiralSequence(self, create, fade):
fade,
run_time = 3
)

def plotPoints(self):

for i, j in zip(X, Y):
self.add(
Dot(self.coords_to_point(
i, j
))
)
self.wait(0.75)

def parametricFunction(self):

return ParametricFunction(
Expand All @@ -60,16 +61,16 @@ def parametricFunction(self):
t_max = ((n // 4) * 2 + (n % 4)) * np.pi
)


def connectPoints(self, X, Y):

def connectPoints(self, X, Y):
'''Similar to plt.scatter(X, Y) in mpl'''
return VMobject().set_points_as_corners(
list(map(
lambda x, y : self.coords_to_point(x, y),
X, Y
))
)

def construct(self):

self.setup_axes(animate = True)
Expand All @@ -81,9 +82,9 @@ def construct(self):
self.connectPoints(X[::2], Y[::2]),
self.connectPoints(X[1::2], Y[1::2])
]

self.plotPoints()

self.logSpiralSequence(Create, Fade)
self.play(ShowCreation(
linePrime,
Expand All @@ -103,4 +104,3 @@ def construct(self):
run_time = 6
)
self.wait(5)

21 changes: 17 additions & 4 deletions src/matplotlibUtils/functions.py → src/mpl/functions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import matplotlib.pyplot as plt
import numpy as np
import math

from matplotlibUtils.zoomFactory import zoomFactory
from mpl.zoomFactory import zoomFactory
from solver import f

def plotAxes(x, y):
ax = plt.figure().add_subplot(1, 1, 1)

ax.spines['left'].set_position(('data', x))
ax.spines['bottom'].set_position(('data', y))
ax.spines['right'].set_position(('data', x - 1))
Expand All @@ -29,17 +31,28 @@ def plotLine(n, lines):
plt.plot(X, Y, 'ro-')

def plotSpiral(n, spiral):
'''Plots Spiral based on cartesion equation
x = r * cos(theta),
y = r * sin(theta),
where r = a * phi^(theta / pi)
'''
theta = np.arange(
-4 * np.pi,
((n // 4) * 2 + (n % 4)) * np.pi,
0.01
)
X = list(map(
lambda x : spiral.a * pow(spiral.p, x / np.pi) * np.cos(x),
lambda x :
spiral.a
* pow(spiral.p, x / np.pi)
* np.cos(x),
theta
))
Y = list(map(
lambda y : spiral.a * pow(spiral.p, y / np.pi) * np.sin(y),
lambda y :
spiral.a
* pow(spiral.p, y / np.pi)
* np.sin(y),
theta
))
plt.plot(X, Y)
3 changes: 1 addition & 2 deletions src/matplotlibUtils/plotter.py → src/mpl/plotter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import matplotlib.pyplot as plt

from matplotlibUtils.functions import *
from mpl.functions import *
from shapes.logSpiral import Spiral
from shapes.point import points
from shapes.line import lines
Expand All @@ -21,4 +21,3 @@ def plot(n, init):
plotSpiral(n, Spiral())
plt.scatter(X, Y)
plt.show()

File renamed without changes.
Loading

0 comments on commit c61032b

Please sign in to comment.