Archive

Posts Tagged ‘graphics’

Python and Lindenmayer System – P2

November 3, 2019 1 comment


Learning : Lindenmayer System P2
Subject: Drawing with python using L-System

In the first part of Lindenmayer System L-System post (Click to Read) we had wrote two functions: one to generate the pattern based on the variables and roles, and one to draw lines and rotate based on the pattern we have.

In this part I will post images of what Art we can generate from L-System
the codes will be the L-system that generate the patterns, so the code will include: the Rules, Angle (Right, Left) Iteration and Starting Variable.


L-System: Koch Curve

L-System: Minkowski Sausage

L-System: … but here the Iteration is: 3

L-System: Again … but here the Iteration is: 3

L-System: Square Sierpinski

L-System: Sierpinski Arrowhead.

L-system: Dragon Curve

L-System: Koch Snowflake

L-System:

L-System:


The possibilities to generate the putters and therefore drawing the output is endless, any slightly changes in the iterations or rotation (+ -) angles will take all output to a new levels. In the coming post, I will use the L-system to generate fractal tree and see what we can get from there.



Follow me on Twitter..





Python Project: Ant Escaping Path

August 29, 2019 Leave a comment


Python simulation project
Python, Graphics and simulation

In this project we assume that we have a square pad and we put an ant in the center of this pad, we let the ant to walk, once it reach any eadgs of the square we move it with our hand to the middle again, say we doing this for x times and each time we colored the ant foots with gray level (light to dark) color. Our project is to write a code to simulate this task (ant movement). 

Enhancement: Here are some enhancement ideas:
1. We can use the Pi or Golden Ratio for the variables.
2. Also we can set a memory facto so with each time we move the Ant to the center the memory will increase so it may use the same path.
3. For the memory, we can set a position (x,y) as a food, and each time it reach the food it’s memory will increase.


The Code

# Create in 27/8/2019 .

import turtle
import random

screen = turtle.Screen()
screen.setworldcoordinates(-700,-700,700,700)
screen.tracer(5)

t1=turtle.Turtle()
t1.speed(0)
t1.penup()

# colors varibles
# r=240, g=240, b=240 is a light gray color

r=240
g=240
b=240
t1.pencolor(r,g,b)

cf = 3 # color increasing factor
ff = 0 # ant forward moving factor
#screen.bgcolor(“black”)

def rand_walk(x,the_t):

the_t.pendown()

# The color will be darker each time

the_t.pencolor(r-(x*cf),g-(x*cf),b-(x*cf))

the_t.forward(20+(ff*x))

def ant_walk ():

x = 1

while x < 7 : # Moving the Ant to the center 7 times

rand_walk(x,t1)

if random.randrange(1,100) %2:

t_ang = random.randrange(10,45)

t1.right(t_ang)

else:

t_ang = random.randrange(10,45)

t1.left (t_ang)

# if the ant reach the square boards then we move it to the center.

if (t1.xcor() > 650 ) or (t1.xcor() 650 ) or (t1.ycor() < -650 ):

t1.penup()

t1.goto(0,0)

x = x + 1

# Calling the Function
ant_walk()


Here is an GIF file



Follow me on Twitter..