### Author Topic: ===[CHALLENGE - hex fractal]===  (Read 3535 times)

0 Members and 1 Guest are viewing this topic.

#### Jeremy Dunn

• Newt
• Posts: 31
##### ===[CHALLENGE - hex fractal]===
« on: March 19, 2018, 01:07:38 AM »
The diagram shows a three stage development of a fractal that is based on the 7 hex unit on the left. One repeats 6 hex units and leaves the central hex empty. The challenge is to write a program that will draw any generation of the pattern given the iteration integer. The program should draw the pattern without repeating edges and calculate points in such a way as to have as little cumulative error as possible. Let the programs begin!

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #1 on: March 19, 2018, 06:37:24 AM »
Another one...
Marko Ribar, d.i.a. (graduated engineer of architecture)

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #2 on: March 19, 2018, 09:04:30 AM »
And yet another one...
Marko Ribar, d.i.a. (graduated engineer of architecture)

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #3 on: March 19, 2018, 02:08:27 PM »
Not exactly what you wanted, but I managed to get this...
Marko Ribar, d.i.a. (graduated engineer of architecture)

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #4 on: March 19, 2018, 02:17:41 PM »
Here are my attempts :

Code - Auto/Visual Lisp: [Select]
1. (defun c:fractals-3sided ( / fill odd even adoc p r rr p1 p2 p3 n k )
2.
3.
4.   (defun fill ( e / hatch )
5.
6.
8.     (vla-appendouterloop hatch (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list (vlax-ename->vla-object e))))
9.   )
10.
11.   (defun odd ( r / p1 p2 p3 ss i e el ) ; p is global
12.
13.
14.     (setq p1 (polar p (/ (* 7 pi) 6) r))
15.     (setq p2 (polar p (/ (* 11 pi) 6) r))
16.     (setq p3 (polar p (/ pi 2) r))
17.     (setq ss (ssget "_A"))
18.     (repeat (setq i (sslength ss))
19.       (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
20.       (setq el (cons e el))
21.     )
22.     (foreach e el
23.       (vla-mirror e (vlax-3d-point p1) (vlax-3d-point p2))
24.     )
25.     (foreach e el
26.       (vla-mirror e (vlax-3d-point p2) (vlax-3d-point p3))
27.     )
28.     (foreach e el
29.       (vla-mirror e (vlax-3d-point p3) (vlax-3d-point p1))
30.     )
31.     (foreach e el
32.       (vla-delete e)
33.     )
34.     (fill (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 3) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) '(210 0.0 0.0 1.0))))
35.   )
36.
37.   (defun even ( r / p1 p2 p3 ss i e el ) ; p is global
38.
39.
40.     (setq p1 (polar p (/ pi 6) r))
41.     (setq p2 (polar p (/ (* 5 pi) 6) r))
42.     (setq p3 (polar p (/ (* 3 pi) 2) r))
43.     (setq ss (ssget "_A"))
44.     (repeat (setq i (sslength ss))
45.       (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
46.       (setq el (cons e el))
47.     )
48.     (foreach e el
49.       (vla-mirror e (vlax-3d-point p1) (vlax-3d-point p2))
50.     )
51.     (foreach e el
52.       (vla-mirror e (vlax-3d-point p2) (vlax-3d-point p3))
53.     )
54.     (foreach e el
55.       (vla-mirror e (vlax-3d-point p3) (vlax-3d-point p1))
56.     )
57.     (foreach e el
58.       (vla-delete e)
59.     )
60.     (fill (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 3) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) '(210 0.0 0.0 1.0))))
61.   )
62.
63.   (initget 1)
64.   (setq p (getpoint "\nPick or specify center point : "))
65.   (initget 7)
66.   (setq rr (getdist p "\nSpecify initial radius of triangle : "))
67.   (setq r (/ rr 2))
68.   (setq p1 (polar p (/ (* 7 pi) 6) rr))
69.   (setq p2 (polar p (/ (* 11 pi) 6) rr))
70.   (setq p3 (polar p (/ pi 2) rr))
71.   (entmake (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 3) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) '(210 0.0 0.0 1.0)))
72.   (initget 7)
73.   (setq n (getint "\nSpecify number of iterations : "))
74.   (setq k 0)
75.   (repeat n
76.     (setq k (1+ k))
77.     (if (= 1 (rem k 2))
78.       (odd (setq r (* 2 r)))
79.       (even (setq r (* 2 r)))
80.     )
81.   )
82.   (princ)
83. )
84.

Code - Auto/Visual Lisp: [Select]
1. (defun c:fractals-4sided ( / fill foo adoc p r rr p1 p2 p3 p4 n )
2.
3.
4.   (defun fill ( e / hatch )
5.
6.
8.     (vla-appendouterloop hatch (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list (vlax-ename->vla-object e))))
9.   )
10.
11.   (defun foo ( r / p1 p2 p3 p4 ss i e el ) ; p is global
12.
13.
14.     (setq p1 (polar p (/ (* 5 pi) 4) r))
15.     (setq p2 (polar p (/ (* 7 pi) 4) r))
16.     (setq p3 (polar p (/ pi 4) r))
17.     (setq p4 (polar p (/ (* 3 pi) 4) r))
18.     (setq ss (ssget "_A"))
19.     (repeat (setq i (sslength ss))
20.       (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
21.       (setq el (cons e el))
22.     )
23.     (foreach e el
24.       (vla-mirror e (vlax-3d-point p1) (vlax-3d-point p2))
25.     )
26.     (foreach e el
27.       (vla-mirror e (vlax-3d-point p2) (vlax-3d-point p3))
28.     )
29.     (foreach e el
30.       (vla-mirror e (vlax-3d-point p3) (vlax-3d-point p4))
31.     )
32.     (foreach e el
33.       (vla-mirror e (vlax-3d-point p4) (vlax-3d-point p1))
34.     )
35.     (foreach e el
36.       (vla-mirror e (vlax-3d-point (polar p1 (/ (* 3 pi) 4) 1.0)) (vlax-3d-point (polar p1 (/ (* 7 pi) 4) 1.0)))
37.     )
38.     (foreach e el
39.       (vla-mirror e (vlax-3d-point (polar p2 (/ (* 5 pi) 4) 1.0)) (vlax-3d-point (polar p2 (/ pi 4) 1.0)))
40.     )
41.     (foreach e el
42.       (vla-mirror e (vlax-3d-point (polar p3 (/ (* 7 pi) 4) 1.0)) (vlax-3d-point (polar p3 (/ (* 3 pi) 4) 1.0)))
43.     )
44.     (foreach e el
45.       (vla-mirror e (vlax-3d-point (polar p4 (/ pi 4) 1.0)) (vlax-3d-point (polar p4 (/ (* 5 pi) 4) 1.0)))
46.     )
47.     (foreach e el
48.       (vla-delete e)
49.     )
50.     (fill (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4) '(210 0.0 0.0 1.0))))
51.   )
52.
53.   (initget 1)
54.   (setq p (getpoint "\nPick or specify center point : "))
55.   (initget 7)
56.   (setq r (getdist p "\nSpecify initial radius of square : "))
57.   (setq rr (/ r 3))
58.   (setq p1 (polar p (/ (* 5 pi) 4) r))
59.   (setq p2 (polar p (/ (* 7 pi) 4) r))
60.   (setq p3 (polar p (/ pi 4) r))
61.   (setq p4 (polar p (/ (* 3 pi) 4) r))
62.   (setq r rr)
63.   (entmake (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4) '(210 0.0 0.0 1.0)))
64.   (initget 7)
65.   (setq n (getint "\nSpecify number of iterations : "))
66.   (repeat n
67.     (foo (setq r (* 3 r)))
68.   )
69.   (princ)
70. )
71.

Code - Auto/Visual Lisp: [Select]
1. (defun c:fractals-6sided ( / fill foo adoc p r rr p1 p2 p3 p4 p5 p6 n )
2.
3.
4.   (defun fill ( e / hatch )
5.
6.
8.     (vla-appendouterloop hatch (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list (vlax-ename->vla-object e))))
9.   )
10.
11.   (defun foo ( r / p1 p2 p3 p4 p5 p6 ss i e el ) ; p is global
12.
13.
14.     (setq p1 (polar p (/ (* 4 pi) 3) r))
15.     (setq p2 (polar p (/ (* 5 pi) 3) r))
16.     (setq p3 (polar p 0.0 r))
17.     (setq p4 (polar p (/ pi 3) r))
18.     (setq p5 (polar p (/ (* 2 pi) 3) r))
19.     (setq p6 (polar p pi r))
20.     (setq ss (ssget "_A"))
21.     (repeat (setq i (sslength ss))
22.       (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
23.       (setq el (cons e el))
24.     )
25.     (foreach e el
26.       (vla-mirror e (vlax-3d-point p1) (vlax-3d-point p2))
27.     )
28.     (foreach e el
29.       (vla-mirror e (vlax-3d-point p2) (vlax-3d-point p3))
30.     )
31.     (foreach e el
32.       (vla-mirror e (vlax-3d-point p3) (vlax-3d-point p4))
33.     )
34.     (foreach e el
35.       (vla-mirror e (vlax-3d-point p4) (vlax-3d-point p5))
36.     )
37.     (foreach e el
38.       (vla-mirror e (vlax-3d-point p5) (vlax-3d-point p6))
39.     )
40.     (foreach e el
41.       (vla-mirror e (vlax-3d-point p6) (vlax-3d-point p1))
42.     )
43.     (foreach e el
44.       (vla-delete e)
45.     )
46.     (fill (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 6) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4) (cons 10 p5) (cons 10 p6) '(210 0.0 0.0 1.0))))
47.   )
48.
49.   (initget 1)
50.   (setq p (getpoint "\nPick or specify center point : "))
51.   (initget 7)
52.   (setq r (getdist p "\nSpecify initial radius of hexagon : "))
53.   (setq rr (/ r 3))
54.   (setq p1 (polar p (/ (* 4 pi) 3) r))
55.   (setq p2 (polar p (/ (* 5 pi) 3) r))
56.   (setq p3 (polar p 0.0 r))
57.   (setq p4 (polar p (/ pi 3) r))
58.   (setq p5 (polar p (/ (* 2 pi) 3) r))
59.   (setq p6 (polar p pi r))
60.   (setq r rr)
61.   (entmake (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 6) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4) (cons 10 p5) (cons 10 p6) '(210 0.0 0.0 1.0)))
62.   (initget 7)
63.   (setq n (getint "\nSpecify number of iterations : "))
64.   (repeat n
65.     (foo (setq r (* 3 r)))
66.   )
67.   (princ)
68. )
69.

HTH., M.R.
« Last Edit: March 19, 2018, 10:59:49 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

#### gile

• Water Moccasin
• Posts: 2493
• Marseille, France
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #5 on: March 19, 2018, 02:35:22 PM »
Hi,

No time to play right now, but here's an old one for Sierpinski triangle.

Code - Auto/Visual Lisp: [Select]
1. (defun drawSierpinski (i p1 p2 p3 / draw mid loop fract)
2.   (defun draw (p1 p2 p3)
3.     (entmake (list '(0 . "SOLID") (cons 10 p1) (cons 11 p2) (cons 12 p3) (cons 13 p3)))
4.   )
5.   (defun mid (p1 p2)
6.     (mapcar '(lambda (x1 x2) (/ (+ x1 x2) 2.)) p1 p2)
7.   )
8.   (defun loop (i p1 p2 p3)
9.     (if (< 0 i)
10.       (fract (1- i) p1 p2 p3)
11.       (draw p1 p2 p3)
12.     )
13.   )
14.   (defun fract (i p1 p2 p3)
15.     (loop i p1 (mid p1 p2) (mid p3 p1))
16.     (loop i (mid p1 p2) p2 (mid p2 p3))
17.     (loop i (mid p3 p1) (mid p2 p3) p3)
18.   )
19.   (loop i p1 p2 p3)
20. )

Testing command:
Code - Auto/Visual Lisp: [Select]
1. (defun c:test (/ i c r)
2.   (and
3.     (setq i (getint "\nNumber of iteration: "))
4.     (setq c (getpoint "\nCenter: "))
5.     (setq r (getdist c "\nRadius: "))
6.     (drawSierpinski
7.       i
8.       (polar c (/ pi 2) r)
9.       (polar c (/ (* 7 pi) 6) r)
10.       (polar c (/ (* 11 pi) 6) r)
11.     )
12.   )
13.   (princ)
14. )
Speaking English as a French Frog

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #6 on: March 19, 2018, 11:02:21 PM »
Here is my revision of 6sided - just slightly more elegant, but result is also different than original request...

Code - Auto/Visual Lisp: [Select]
1. (defun c:fractals-6sided-new ( / fill foo adoc p r rr p1 p2 p3 p4 p5 p6 n )
2.
3.
4.   (defun fill ( e / hatch )
5.
6.
8.     (vla-appendouterloop hatch (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list (vlax-ename->vla-object e))))
9.   )
10.
11.   (defun foo ( r / p1 p2 p3 p4 p5 p6 ss i e el ) ; p is global
12.
13.
14.     (setq p1 (polar p (/ (* 4 pi) 3) r))
15.     (setq p2 (polar p (/ (* 5 pi) 3) r))
16.     (setq p3 (polar p 0.0 r))
17.     (setq p4 (polar p (/ pi 3) r))
18.     (setq p5 (polar p (/ (* 2 pi) 3) r))
19.     (setq p6 (polar p pi r))
20.     (setq ss (ssget "_A"))
21.     (repeat (setq i (sslength ss))
22.       (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
23.       (setq el (cons e el))
24.     )
25.     (foreach e el
26.       (vla-mirror e (vlax-3d-point p1) (vlax-3d-point p2))
27.     )
28.     (foreach e el
29.       (vla-mirror e (vlax-3d-point p2) (vlax-3d-point p3))
30.     )
31.     (foreach e el
32.       (vla-mirror e (vlax-3d-point p3) (vlax-3d-point p4))
33.     )
34.     (foreach e el
35.       (vla-mirror e (vlax-3d-point p4) (vlax-3d-point p5))
36.     )
37.     (foreach e el
38.       (vla-mirror e (vlax-3d-point p5) (vlax-3d-point p6))
39.     )
40.     (foreach e el
41.       (vla-mirror e (vlax-3d-point p6) (vlax-3d-point p1))
42.     )
43.     (foreach e el
44.       (if (= (cdr (assoc 0 (entget (vlax-vla-object->ename e)))) "HATCH")
45.         (vla-delete e)
46.         (fill (vlax-vla-object->ename e))
47.       )
48.     )
49.   )
50.
51.   (initget 1)
52.   (setq p (getpoint "\nPick or specify center point : "))
53.   (initget 7)
54.   (setq r (getdist p "\nSpecify initial radius of hexagon : "))
55.   (setq rr (/ r 3))
56.   (setq p1 (polar p (/ (* 4 pi) 3) r))
57.   (setq p2 (polar p (/ (* 5 pi) 3) r))
58.   (setq p3 (polar p 0.0 r))
59.   (setq p4 (polar p (/ pi 3) r))
60.   (setq p5 (polar p (/ (* 2 pi) 3) r))
61.   (setq p6 (polar p pi r))
62.   (setq r rr)
63.   (entmake (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 6) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4) (cons 10 p5) (cons 10 p6) '(210 0.0 0.0 1.0)))
64.   (initget 7)
65.   (setq n (getint "\nSpecify number of iterations : "))
66.   (repeat n
67.     (foo (setq r (* 3 r)))
68.   )
69.   (princ)
70. )
71.

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

#### gile

• Water Moccasin
• Posts: 2493
• Marseille, France
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #7 on: March 20, 2018, 11:38:47 AM »
Code - Auto/Visual Lisp: [Select]
1. (defun hexfractal (i pts / draw loop fract)
2.   (defun draw (pts)
3.         '((0 . "LWPOLYLINE")
4.           (100 . "AcDbEntity")
5.           (100 . "AcDbPolyline")
6.           (90 . 6)
7.           (70 . 1)
8.          )
9.         (mapcar '(lambda (p) (cons 10 p)) pts)
10.       )
11.     )
12.   )
13.   (defun loop (i pts)
14.     (if (< 0 i)
15.       (fract (1- i) pts)
16.       (draw pts)
17.     )
18.   )
19.   (defun fract (i pts / d)
20.     (setq d (/ (distance (car pts) (cadr pts)) 3))
21.       '(lambda (p1 p2 / a)
22.          (setq a (angle p1 p2))
23.          (loop i
24.                  '(lambda (n) (setq p1 (polar p1 (+ a (* k n)) d)))
25.                  '(0 0 1 2 3 4)
26.                )
27.          )
28.        )
29.       pts
30.       (append (cdr pts) (list (car pts)))
31.     )
32.   )
33.   (setq k (/ pi 3))
34.   (loop i pts)
35. )
36.
37. (defun c:test (/ i c r a)
38.   (and
39.     (setq i (getint "\nNumber of iterations: "))
40.     (setq c (getpoint "\nCenter: "))
41.     (setq r (getdist c "\nRadius: "))
42.     (setq a (/ pi 6))
43.     (hexfractal
44.       i
45.       (mapcar '(lambda (x) (polar c (* x a) r))
46.               '(1 3 5 7 9 11)
47.       )
48.     )
49.   )
50.   (princ)
51. )

Speaking English as a French Frog

#### gile

• Water Moccasin
• Posts: 2493
• Marseille, France
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #8 on: March 20, 2018, 12:45:11 PM »
Slightly different.

Code - Auto/Visual Lisp: [Select]
1. (defun hexfractal (i pts / draw loop fract c)
2.   (defun draw (pts)
3.         '((0 . "LWPOLYLINE")
4.           (100 . "AcDbEntity")
5.           (100 . "AcDbPolyline")
6.           (90 . 6)
7.           (70 . 1)
8.          )
9.         (mapcar '(lambda (p) (cons 10 p)) pts)
10.       )
11.     )
12.   )
13.   (defun loop (i pts)
14.     (if (< 0 i)
15.       (fract (1- i) pts)
16.       (draw pts)
17.     )
18.   )
19.   (defun fract (i pts / d)
20.     (setq d (/ (distance (car pts) (cadr pts)) 3.))
21.       '(lambda (p1 p2 / a)
22.          (setq a (angle p1 p2))
23.          (loop i
24.                (mapcar '(lambda (k) (setq p1 (polar p1 (+ (* k c) a) d)))
25.                        '(0 1 2 3 4 5)
26.                )
27.          )
28.        )
29.       pts
30.       (append (cdr pts) (list (car pts)))
31.     )
32.   )
33.   (setq c (/ pi 3.))
34.   (loop i pts)
35. )
Speaking English as a French Frog

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #9 on: March 20, 2018, 01:47:59 PM »
Hi Gilles...

I've finished what OP wanted...

Code - Auto/Visual Lisp: [Select]
1. (defun c:fractals-6sided-new-new ( / fill array odd even adoc p r p1 p2 p3 p4 p5 p6 e n k )
2.
3.
4.   (defun fill ( e / hatch )
5.
6.
8.     (vla-appendouterloop hatch (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list (vlax-ename->vla-object e))))
9.   )
10.
11.   (defun array ( e p n / da k )
12.
13.
14.     (setq da (/ (* 2 pi) n))
15.     (setq k 0)
16.     (repeat (1- n)
17.       (setq k (1+ k))
18.       (vla-copy (vlax-ename->vla-object e))
19.       (vla-rotate (vlax-ename->vla-object (entlast)) (vlax-3d-point p) (* k da))
20.     )
21.   )
22.
23.   (defun odd ( n / ss i e el p1 p2 ell ) ; p is global ; r is global
24.
25.
26.     (setq ss (ssget "_A"))
27.     (repeat (setq i (sslength ss))
28.       (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
29.       (setq el (cons e el))
30.     )
31.     (setq p1 (polar (polar p (/ (* 5 pi) 3) (* 2 r (if (= n 1) 1 (expt 7.0 (1- n))))) 0.0 (* r (if (= n 1) 1 (expt 7.0 (1- n))))))
32.     (setq p2 (polar (polar p pi (* 2 r (if (= n 1) 1 (expt 7.0 (1- n))))) (/ (* 4 pi) 3) (* r (if (= n 1) 1 (expt 7.0 (1- n))))))
33.     (foreach e el
34.       (vla-copy e)
35.       (vla-move (vlax-ename->vla-object (entlast)) (vlax-3d-point p1) (vlax-3d-point p2))
36.       (setq ell (cons (entlast) ell))
37.     )
38.     (foreach e ell
39.       (array e p 6)
40.     )
41.     (foreach e el
42.       (if (= (cdr (assoc 0 (entget (vlax-vla-object->ename e)))) "HATCH")
43.         (vla-delete e)
44.         (fill (vlax-vla-object->ename e))
45.       )
46.     )
47.   )
48.
49.   (defun even ( n / ss i e el p1 p2 ell ) ; p is global ; r is global
50.
51.
52.     (setq ss (ssget "_A"))
53.     (repeat (setq i (sslength ss))
54.       (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
55.       (setq el (cons e el))
56.     )
57.     (setq p1 (polar (polar p (/ (* 5 pi) 3) (* 4 r (if (= n 1) 1 (expt 7.0 (1- n))))) 0.0 (* 2 r (if (= n 1) 1 (expt 7.0 (1- n))))))
58.     (setq p2 (polar (polar p (/ pi 2) (* 3 (/ (* r (sqrt 3.0)) 2) (if (= n 1) 1 (expt 7.0 (1- n))))) pi (* 6.5 r (if (= n 1) 1 (expt 7.0 (1- n))))))
59.     (foreach e el
60.       (vla-copy e)
61.       (vla-move (vlax-ename->vla-object (entlast)) (vlax-3d-point p1) (vlax-3d-point p2))
62.       (setq ell (cons (entlast) ell))
63.     )
64.     (foreach e ell
65.       (array e p 6)
66.     )
67.     (foreach e el
68.       (if (= (cdr (assoc 0 (entget (vlax-vla-object->ename e)))) "HATCH")
69.         (vla-delete e)
70.         (fill (vlax-vla-object->ename e))
71.       )
72.     )
73.   )
74.
75.   (initget 1)
76.   (setq p (getpoint "\nPick or specify center point : "))
77.   (initget 7)
78.   (setq r (getdist p "\nSpecify initial radius of hexagon : "))
79.   (setq p1 (polar p (/ (* 4 pi) 3) r))
80.   (setq p2 (polar p (/ (* 5 pi) 3) r))
81.   (setq p3 (polar p 0.0 r))
82.   (setq p4 (polar p (/ pi 3) r))
83.   (setq p5 (polar p (/ (* 2 pi) 3) r))
84.   (setq p6 (polar p pi r))
85.   (setq e (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 6) (cons 70 (1+ (* 128 (getvar 'plinegen)))) (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4) (cons 10 p5) (cons 10 p6) '(210 0.0 0.0 1.0))))
86.   (vla-copy (vlax-ename->vla-object e))
87.   (vla-move (vlax-ename->vla-object (entlast)) (vlax-3d-point p2) (vlax-3d-point p6))
88.   (array (entlast) p 6)
89.   (fill e)
90.   (initget 7)
91.   (setq n (getint "\nSpecify number of iterations : "))
92.   (setq k 0)
93.   (repeat n
94.     (setq k (1+ k))
95.     (if (= 1 (rem k 2))
96.       (odd (1+ (/ k 2)))
97.       (even (/ k 2))
98.     )
99.   )
100.   (princ)
101. )
102.

M.R.
« Last Edit: March 20, 2018, 11:00:16 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

#### gile

• Water Moccasin
• Posts: 2493
• Marseille, France
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #10 on: March 20, 2018, 02:14:12 PM »
Nice!
Speaking English as a French Frog

#### Jeremy Dunn

• Newt
• Posts: 31
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #11 on: March 20, 2018, 03:15:52 PM »
You guys are too quick! Do you get any work done during the day?    Here's a variation of the hexfractal that you can try that looks simpler but has its own problems to solve. This time draw the outside perimeter as normal but replace the white filled areas with white filled circles that just fit inside the normal jagged holes.

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #12 on: March 20, 2018, 11:06:37 PM »
I have to say that there were lacks in my last posted code - formulas for p1 and p2 in both subs (odd) and (even) were wrong... Now updated code and checked for 6 iterations (5 hours on my slow PC) and it looked fine which means that now p1 and p2 are correct... BTW. DWG is ab 125 MB... But no such astonishing effect with fractals - even 4 iterations is enough to see it good like in my posted picture... Only its more dense - you have to zoom in and pan more...

Gilles's subs I haven't checked, but I guess they are fine...

Regards, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)

#### gile

• Water Moccasin
• Posts: 2493
• Marseille, France
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #13 on: March 21, 2018, 05:28:00 AM »
What OP wants in a scripting way.

Code - Auto/Visual Lisp: [Select]
1. (defun c:hexfract (/ *error* l i c n a d)
2.   (defun *error* (msg)
3.     (and msg
4.          (/= msg "Function cancelled")
5.          (prompt (strcat "\nError: " msg))
6.     )
7.     (foreach v '(clayer cmdecho osmode)
8.       (setvar v (eval v))
9.       (set v nil)
10.     )
11.     (princ)
12.   )
13.   (foreach p '((clayer . "0") (cmdecho . 0) (osmode . 0))
14.     (set (car p) (getvar (car p)))
15.     (setvar (car p) (cdr p))
16.   )
17.   (if (and (setq i (getint "\nNumber of iterations: "))
18.            (setq c (getpoint "\nCenter: "))
19.       )
20.       (setq n 1
21.             a 0
22.             d (sqrt 3)
23.       )
24.       (or (tblsearch "BLOCK" "hex0")
25.           (command-s "_.polygon" 6 c "_inscribe" (polar c (/ pi 2) 1))
26.           (command-s "_.block" "hex0" c (entlast) "")
27.       )
28.       (repeat i
29.         (if (not (tblsearch "BLOCK" (strcat "hex" (itoa n))))
31.             (command-s "_.zoom" "_window" (polar c (* pi 1.25) d) (polar c (* pi 0.25) d))
32.             (repeat 6
33.               (command-s "_.insert" (strcat "hex" (itoa (1- n))) (polar c a d) 1. 1. 0.)
35.               (setq a (+ a (/ pi 3)))
36.             )
37.             (command-s "_bhatch" "_properties" "solid" c "")
39.             (command-s "_.block" (strcat "hex" (itoa n)) c s "")
40.           )
41.         )
42.         (setq n (1+ n)
43.               a (+ a (atan (sqrt 3) 5))
44.               d (* d (sqrt 7))
45.         )
46.       )
47.       (command-s "_.insert" (strcat "hex" (itoa (1- n))) c 1. 1. 0.)
48.       (command-s "_chprop" (entlast) "" "_layer" clayer "")
49.       (command-s "_.zoom" "0.4x")
50.     )
51.   )
52.   (*error* nil)
53. )

Speaking English as a French Frog

#### ribarm

• Gator
• Posts: 3180
• Marko Ribar, architect
##### Re: ===[CHALLENGE - hex fractal]===
« Reply #14 on: March 23, 2018, 03:12:10 PM »
When I replace (command-s) with (vl-cmdf) on A2014 it works, but on A2017 it doesn't... Can you explain?
Thanks for your input... It works significantly faster with block command and insert.

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)