-
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
Showing
4 changed files
with
155 additions
and
28 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 |
---|---|---|
@@ -1,30 +1,93 @@ | ||
from math import * | ||
from Lib.NewDEGraphics import * | ||
|
||
win = DEGraphWin("Koch Curve", 600, 600, showScrollbar = False) | ||
class KochCurve: | ||
width = 600 | ||
height = 600 | ||
|
||
def length(lvl): | ||
return (1/3) ** lvl | ||
targetLevel = 4 | ||
angle = 60 | ||
|
||
currentPoints = [] | ||
position = (0, 0) | ||
|
||
# Draw the Koch Curve recursively | ||
def drawKochCurve(origin, angle, length, angleChange, direction, maxDepth, depth, i): | ||
if depth == maxDepth: | ||
return | ||
else: | ||
# Draw the line | ||
line = Line(origin[0], origin[1], origin[0] + length * cos(radians(angle)), origin[1] + length * sin(radians(angle)))) | ||
line.draw() | ||
|
||
# Draw the next line | ||
drawKochCurve((origin[0] + length * cos(radians(angle)), origin[1] + length * sin(radians(angle))), angle + angleChange * direction, length, angleChange, direction, maxDepth, depth + 1, i) | ||
# Draw the next line | ||
drawKochCurve((origin[0] + length * cos(radians(angle)), origin[1] + length * sin(radians(angle))), angle - angleChange * direction, length, angleChange, direction, maxDepth, depth + 1, i) | ||
def __init__(self): | ||
self.win = DEGraphWin("Koch Curve", self.width, self.height, showScrollbar = False) | ||
|
||
with self.win: | ||
self.stack = Stack() | ||
with self.stack: | ||
self.targetLevelSlider = Slider(0, 10, self.targetLevel, self.width, 30, 1) | ||
self.targetLevelSlider.setInputCallback(self.setTargetLevel) | ||
|
||
self.angleSlider = SliderWithTextInput(0, 90, self.angle, self.width, 30, 1, self.setAngle, labelText = "Angle: ") | ||
|
||
self.canvas = Canvas(width = self.width, height = self.height) | ||
with self.canvas: | ||
# Draw base line | ||
self.line = Line(0, self.height, self.width, self.height, 'black') | ||
self.line.draw() | ||
|
||
# Create curve polygon | ||
self.curvePolygon = Polygon([(0, 0), (0, self.height), (self.width, self.height)], 'red', outline = 'black') | ||
self.drawKochCurve() | ||
|
||
self.win.update() | ||
self.win.mainloop() | ||
|
||
# Draw Koch Curve | ||
def drawKochCurve(self): | ||
self.position = (0, 0) | ||
self.currentPoints = [] | ||
self.calculateKochCurve(0, 1.0, self.targetLevel) | ||
|
||
# Convert the points to canvas coordinates | ||
for i in range(len(self.currentPoints)): | ||
self.currentPoints[i] = self.convertPoint(self.currentPoints[i]) | ||
|
||
# Convert the points into an actual polygon | ||
self.curvePolygon.setPoints(self.currentPoints) | ||
self.curvePolygon.draw() | ||
|
||
# Recursively calculate Koch Curve | ||
def calculateKochCurve(self, currentAngle, length, level): | ||
if level == 0: | ||
newPt = self.projectPoint(currentAngle, length) | ||
self.currentPoints.extend([self.position, newPt]) | ||
self.position = newPt | ||
else: | ||
scaleFactor = 1.0 / (2.0 * (1.0 + cos(radians(self.angle)))) | ||
|
||
self.calculateKochCurve(currentAngle, length * scaleFactor, level - 1) | ||
self.calculateKochCurve(currentAngle + self.angle, length * scaleFactor, level - 1) | ||
self.calculateKochCurve(currentAngle - self.angle, length * scaleFactor, level - 1) | ||
self.calculateKochCurve(currentAngle, length * scaleFactor, level - 1) | ||
|
||
with win: | ||
canvas = Canvas() | ||
with canvas: | ||
# Draw Koch Curve | ||
drawKochCurve((300, 300), 0, 300, 60, 1, 5, 0, 0) | ||
def projectPoint(self, directionAngle, length): | ||
xPt = self.position[0] + length * cos(radians(directionAngle)) | ||
yPt = self.position[1] + length * sin(radians(directionAngle)) | ||
|
||
return (xPt, yPt) | ||
|
||
# Target level slider callback | ||
def setTargetLevel(self, value): | ||
self.targetLevel = self.targetLevelSlider.value | ||
|
||
self.drawKochCurve() | ||
|
||
# Angle callback | ||
def setAngle(self, value): | ||
self.angle = float(value) | ||
|
||
self.drawKochCurve() | ||
|
||
# Convert point to canvas coordinates | ||
def convertPoint(self, point): | ||
x, y = point | ||
drawX = x * self.width | ||
drawY = (1 - y) * self.height | ||
|
||
return (drawX, drawY) | ||
|
||
win.update() | ||
win.mainloop() | ||
if __name__ == '__main__': | ||
KochCurve() |
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
Binary file not shown.
Binary file modified
BIN
+1.89 KB
(100%)
Programs/Class/CantorSet/Lib/__pycache__/NewDEGraphics.cpython-310.pyc
Binary file not shown.