[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Odp: LOGO-L> Sierpinski Curves]



Andrzej Baczynski wrote:
> 
> Frank wrote:
> <snip> 
> > I feel however that my code isn't optimal. It is a literal translation
> > of the algorithm to draw the curve.
> 
> I don't know, what algorithm do You consider. There are 4 recursive
> procedures in Your program. This solution is very similar to that used by
> Niklaus Wirth in his book "Algorithms + Data Structures = Programs"
> (wonderful book indeed, and written by Pascal programming language
> creator). 

I should have said 'an' algorithm of course.  The idea for the curve
came from the web site I cited in the code:
http://www.bethel.edu/Academics/MathCS/Sierpinski/SierpinskiFrame.html
He does reference the book by Wirth so perhaps that is way they are
similar.

I have run your new code and it does keep the curve in the unit square.
I haven't had tine to really look into it but it does appear simpler
than my code. It's always good to see a problem solved in a different
way, it forces us see in new ways. 

> BTW1 - this is neither Sierpinski nor Steinhaus in Microsoft Encarta. Mr
> Gates - I don't love you any more.
Not having loved Mr Gates since early MSDOG I'll add this to my reasons
for feeling as I do.
> 
> BTW2 -  It is an interesting variation of Sierpinski's curve introduced by
> Wirth in his book. I attach this figure without Logo code. Have a fun an
> discover it :-)
> 
Looks good,  I look forward to playing around with it, thanks.

In the meantime I have attached a revised version of my code for the
curve.  It allows the different line segments to be drawn in different
colors.  This helps a lot in following the recursion taking place in the
program.   I have also renamed the segment procedures to Side i.e. aSeg
is now aSide.  This fits better with the spirit of the algorithm.  If
you map the a,b,c and d Sides to the top, right, bottom and left sides
of the square respectively the program may make more sense. 

regards
-- 
Frank Caggiano
caggiano@atlantic.net
http://www.atlantic.net/~caggiano


to sierp :len :level :color
;; :len sets the oveall size of the finished square.
;; :level controls the recusion 
;; :color is either 0 or 1, if 0 no color is set for the lines.
;;
;; The length equation was obtained from:
;;
http://www.bethel.edu/Academics/MathCS/Sierpinski/SierpinskiFrame.html

	make "len :len * 1 / ((power 2 (:level + 1)) - 2 + ((power 2 :level) *
sqrt 2))
	
	ifelse :color = 1 [make "lineDrawer "colorLine]~
			[make "lineDrawer "bwLine]

	seth 135

	aSide :len :level
	rt 90 fd :len rt 90
	bSide :len :level
	rt 90 fd :len rt 90
	cSide :len :level
	rt 90 fd :len rt 90
	dSide :len :level
	rt 90 fd :len 
end

to bwLine :len :colorVector
;; ignore colorVector in black and white version
;;
	localmake "inPC pencolor
	setpencolor [0 0 0]
	fd :len lt 45 fd :len fd :len lt 45 fd :len 
	setpencolor :inPc
end

to colorLine :len :colorVector
	localmake "inPC pencolor
	setpencolor :colorVector
	fd :len lt 45 fd :len fd :len lt 45 fd :len 
	setpencolor :inPC
end

to aSide :len :level
	if :level < 1 [(invoke :lineDrawer :len [255 0 0])  stop]

	aSide :len  :level - 1
	rt 90 fd :len  rt 90
	bSide :len  :level - 1
	lt 45 fd :len  fd :len lt 45
	dSide :len  :level - 1
	rt 90 fd :len rt 90
	aSide :len  :level - 1

end

to bSide :len :level
	if :level < 1 [(invoke :lineDrawer :len [0 0 255]) stop]

	bSide :len  :level - 1
	rt 90 fd :len rt 90
	cSide :len  :level - 1
	lt 45 fd :len fd :len lt 45
	aSide :len  :level - 1
	rt 90 fd :len rt 90
	bSide :len  :level - 1
	
end

to cSide :len :level
	if :level < 1 [(invoke :lineDrawer :len [0 255 0]) stop]

	cSide :len  :level - 1
	rt 90 fd :len rt 90 
	dSide :len  :level - 1
	lt 45 fd :len fd :len lt 45 
	bSide :len  :level - 1
	rt 90 fd :len rt 90
	cSide :len   :level - 1
end

to dSide :len :level
	if :level < 1 [(invoke :lineDrawer :len [0 255 255]) stop]

	dSide :len  :level - 1
	rt 90 fd :len rt 90
	aSide :len  :level - 1
	lt 45 fd :len fd :len lt 45
	cSide :len  :level - 1
	rt 90 fd :len rt 90
	dSide :len  :level - 1
end
---------------------------------------------------------------
Please post messages to the Logo forum to logo-l@gsn.org.  Mail
questions about the list administration to logofdn@gsn.org.  To
unsubscribe send    unsubscribe logo-l    to majordomo@gsn.org.



Global SchoolNet Foundation - Linking Kids Around the World!
Copyright GSN - All Rights Reserved - Comments & Questions
Visit GSN's Global Schoolhouse for more exciting learning resources!
Search our Site - Home