Home > Software engineering >  Python Turtle Shape
Python Turtle Shape

Time:10-17

I'm trying to draw a tilted square inside a square using python turtle. However I'm not getting my desired shape as seen in the figure

enter image description here

I also have to use the functions square() and tiltsquare() for the squareinsquare() function.

This is my code as of yet -

import turtle as t
t.speed(1)

def square(x, y, side):
    t.setpos(x,y)
    for i in range(4):
        t.forward(side)
        t.right(90)


#square(0,0,100)    

def tiltsquare(x, y, side):
    t.setpos(x,y)
    t.left(45)
    for i in range(4):
        t.forward(side)
        t.right(90)

#tiltsquare(-50,-50,100)  



def squareinsquare(x,y,side):
    square(0,0,200)
    t.forward(100)
    tiltsquare(0/2,0/2, 100)

squareinsquare(0,0,0)

which outputs

enter image description here

CodePudding user response:

from turtle import Turtle, Screen

t = Turtle()
screen = Screen()

def square(x, y, side):
    t.goto(x, y)
    for i in range(4):
        t.forward(side)
        t.right(90)

def square_in_square():
    x_pos = t.xcor()
    y_pos = t.ycor()
    square(0, 0, 200)
    t.left(45)
    square(x_pos, y_pos - 100, 140)

square_in_square()

screen.exitonclick()

CodePudding user response:

You need to figure out where the inner/tilted square should start, and how long the sides are:

from turtle import Turtle, Screen
import math

t = Turtle()
s = Screen()
t.speed(3)

def square(x, y, side):
    t.setpos(x,y)
    for i in range(4):
        t.forward(side)
        t.right(90)

def tiltsquare(x, y, side):
    t.left(45)
    square(x, y, side)

def squareinsquare(x, y, side):
    square(x, y, side)

    # the middle of the left side is (x, y - side/2)
    # the length of the sides in the tilted square is
    # (using pythagoras)
    half = side / 2
    b = math.sqrt(half**2   half**2)

    tiltsquare(x, y - side/2, b)

squareinsquare(0, 0, 200)
screen.exitonclick()

output

enter image description here

Just for fun... if you want to draw the square-in-square at an angle, you need to generalize the starting point for the inner square:

def squareinsquare(x, y, side, angle):
    t.right(angle)
    square(x, y, side)

    half = side / 2

    # the starting x,y of the inner/tilted square
    angle = math.radians(angle)
    x -= half * math.sin(angle)
    y -= half * math.cos(angle)
    
    # the length of the sides in the tilted square is
    # (using pythagoras)
    b = math.sqrt(half**2   half**2)

    tiltsquare(x, y, b)


squareinsquare(0, 0, 200, 25)

giving

enter image description here

x and y are found by solving the triangle abc:

enter image description here

CodePudding user response:

import turtle as t
t.speed(1)


side = 200 #Set the desired outside square side length


def square(side):
    for i in range(4):
        t.forward(side)
        t.right(90)


def tiltsquare(tilted_side):
    t.forward(tilted_side / (2 ** 0.5))
    t.right(45)
    for i in range(4):
        t.forward(tilted_side)
        t.right(90)


def squareinsquare(side):
    square(side)
    tiltsquare(side / 2 ** 0.5)


squareinsquare(side)

Check this out, sure this will work

Happy learning!

CodePudding user response:

import turtle

skk = turtle.Turtle()

for i in range(4):

skk.forward(50)

skk.right(90)

 

turtle.done()

  • Related