Skip to content

Commit

Permalink
fixed the koch curve project
Browse files Browse the repository at this point in the history
  • Loading branch information
AIP21 committed Apr 15, 2023
1 parent d76f466 commit 90b2abf
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 28 deletions.
109 changes: 86 additions & 23 deletions Programs/Class/CantorSet/KochCurve.py
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()
74 changes: 69 additions & 5 deletions Programs/Class/CantorSet/Lib/NewDEGraphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class DEGraphWin(tk.Tk):

dragStart = (0, 0)

def __init__(self, title = "Window", width = 500, height = 500, showScrollbar = False, edgePadding = [10, 10, 10, 10], **kw):
def __init__(self, title = "Window", width = 500, height = 500, showScrollbar = False, edgePadding = [0, 0, 0, 0], **kw):
tk.Tk.__init__(self, **kw)
self.title(title)
self.kw = kw
Expand Down Expand Up @@ -1032,7 +1032,7 @@ class TextBox(tk.Frame):
executeOnType: Whether or not to execute the command function on every key press
padding: Padding around the text box
cornerRadius: Corner radius of the text box
inputType: Type of input. Can be 'text', 'int', or 'float'
inputType: Type of input. Can be 'text', 'int', or 'decimal'
color: Color of the text box
textColor: Color of the text
'''
Expand Down Expand Up @@ -1164,7 +1164,7 @@ def validateChar(self, newText):

if self.executeOnType:
self.submit(None, newText)
elif self.type == 'float' and isFloat(newText):
elif self.type == 'decimal' and isFloat(newText):
# Set text color to black
self.entry.config(foreground = colorRGB(*self.textColor))
self.validInput = True
Expand Down Expand Up @@ -1195,6 +1195,10 @@ def submit(self, value, newText = None):
if not newText:
_root.focus()

def setInputCallback(self, callback, *args):
self.validateCommand = callback
self.commandArgs = args

@property
def text(self):
return self.textvariable.get()
Expand Down Expand Up @@ -2105,6 +2109,66 @@ def disable(self):
# Hide the thumb
self.thumb.undraw()

class SliderWithTextInput(Flow):
def __init__(self, start, end, value, width, height, step = 0, command = "", labelText = "", suffix = "", padding = [20, 10], cornerRadius = 20, color = (150, 150, 250), backgroundColor = (100, 100, 100), labelFont = "Arial 8 bold", labelColor = (255, 255, 255),**kwargs):
super().__init__()

self.command = command

with self:
# Create a label
self.label = Label(labelText)

# Create the slider
self.slider = Slider(start, end, value, width - 100, height, step, suffix, padding, cornerRadius, color, backgroundColor, labelFont, labelColor, **kwargs)

# Create the text input
self.textInput = TextBox(100, height, str(value), command = self.onTextInput, font = labelFont)

# Set the text input value
self.textInput.text = str(value)

# Set the slider callback
self.slider.setInputCallback(self.onSliderInput)

def getValue(self):
return self.slider.getValue()

def onTextInput(self, event):
# Set the slider value
self.slider.setValue(float(self.textInput.text))

if self.command != None:
self.command(self.slider.getValue())

def onSliderInput(self, event):
# Set the text input value
self.textInput.text = str(self.slider.getValue())

if self.command != None:
self.command(self.slider.getValue())

def setValue(self, newValue):
self.slider.setValue(newValue)
self.textInput.text = str(newValue)

def enable(self):
self.slider.enable()
self.textInput.enable()

def disable(self):
self.slider.disable()
self.textInput.disable()

def setInputCallback(self, func):
'''
Set the function to call when the user drags the slider
Args:
func: The function to call
'''
self.command = func

class Popup:
def __init__(self, widget, text, closeButtonText = "X", xOffset = 10, yOffset = 10, **kwargs):
"""
Expand Down Expand Up @@ -2364,10 +2428,10 @@ def __init__(self, points, color, outline = None, canvas = None):
def _draw(self):
self.id = self.canvas.create_polygon(self.points, outline = self.outline, fill = self.color)

def get_points(self):
def getPoints(self):
return self.points

def set_points(self, points):
def setPoints(self, points):
self.points = points

# Image
Expand Down
Binary file added Programs/Class/CantorSet/Lib/NewDEGraphics.zip
Binary file not shown.
Binary file not shown.

0 comments on commit 90b2abf

Please sign in to comment.