;;;series summation ;;; SUMSQR_3.LSP ;;; ;;; Copyright 2006 Takaya Iwamoto ;;; Oct 4,2006 Takaya Iwamoto used sumsqr_2.lsp as a template ;;; Oct 6,2006 animation done. slow_move, slow_rotate ;;;**ref** ;;;J.H.Conway, "The book of Numbers", p 45 ;;; (defun c:sumsqr_3 ( / last_1 last_2 ) (setup_sumsqr3) (setq nstep 2) (while (= (Yes_or_No "Go to next step ?") "_Y") (n_balls nstep) (setq nstep (1+ nstep)) ) ;;;end of while loop (make_jpg) (setq nstep (1- nstep)) ;;;copy the triangle to the right and rotate 120 degrees. (command "_.delay" 1000) (alert "\nCopy the triangle to the right and rotate" ) (copy_2_triangle) (make_jpg) ;;;draw plus and equal signs between triangles (command "_.delay" 1000) (alert "\nAdd Plus and Equal signs") (draw_plus_equal) (make_jpg) ;;;draw the triangle with sum (nstep + 1) in the center (command "_.delay" 1000) (alert "\nDraw a triangle with sum") (sum_triangle) (make_jpg) ;;;display texts and arrows (command "_.delay" 1000) (alert "\nDraw a title text") (final_title) (make_jpg) (reset_sysvar) ) ;;;C:sumsqr_3 ;;; ;;; ;;;COPY_2_TRIANGLE ;;; (defun copy_2_triangle() ;;compute the locations where triangle will be placed bases on the base ;;length of the triangle. (setq pnt_1 (shift_pnt pnt_right (list (+ base_length 2.) 0.)) pnt_2 (shift_pnt pnt_1 (list (+ base_length 2.) 0.)) down_shift (* nstep space_down) pt_1 (shift_pnt pnt_org (list (* 0.5 base_length) 0.)) ) (command "_.delay" 1000) ;;make two selection sets (setq ss1 (ssget "X") ) ;;************************************** (alert "\nCopy the first triangle") ;;copy the first triangle (command "_.copy" ss1 "" pnt_org pnt_org) (slow_move ss1 pnt_org pnt_1 3 500) (command "_.delay" 500) ;;define selection sets SS2 & SS3 for rotation (setq pnt_cr12 (shift_pnt pnt_right (list (+ (* 0.5 base_length) 2.) down_shift)) pnt_cr11 (shift_pnt pnt_right (list (+ (* 1.5 base_length) 2.) 0.)) pnt_cr22 (shift_pnt pnt_org (list (+ (* 2.0 base_length) 4.) (* 2. down_shift))) pnt_cr21 (shift_pnt pnt_org (list (+ (* 3.0 base_length) 4.) down_shift)) pnt_org_2 (shift_pnt pnt_org (list (+ 2. base_length) 0.)) pnt_c11 (shift_pnt pnt_org (list (+ 2. (* 1.5 base_length)) 0.)) pnt_c12 (shift_pnt pnt_right (list 2. 0.)) ) ;;make selection for the first triangle (setq ss2 (ssget "C" pnt_cr11 pnt_cr12)) (Alert "\nRotate 120 degrees clockwise") ;;rotate the first triangle 120. clockwise (slow_rotate ss2 pnt_1 -120. 4 500) ;;************************************** (alert "\nCopy the second triangle") ;;copy the second triangle (setq ss1a (ssget "C" pnt_c11 pnt_c12)) (command "_.copy" ss1a "" pnt_org_2 pnt_org_2) (slow_move ss1a pnt_org_2 pnt_2 3 500) (command "_.delay" 500) (command "_.regen") (alert "\nRotate 120 degrees clockwise") ;;make selection for the second triangle (setq ss3 (ssget "C" pnt_cr21 pnt_cr22)) ;;rotate the second triangle 120. clockwise (slow_rotate ss3 pnt_2 -120. 4 500) );;;COPY_2_TRIANGLE ;;; ;;;DRAW_PLUS_EQUAL ;;; (defun draw_plus_equal() (setq pnt_plus_1 (shift_pnt pnt_right (list 0.5 (- -1. (* 0.5 down_shift) ))) pnt_Plus_2 (shift_pnt pnt_plus_1 (list (+ 2. base_length) 0.)) pnt_equal (shift_pnt pnt_org (list 0. (* 1.5 down_shift))) ) (textdisplay "+" pnt_plus_1 1.5 0.) (textdisplay "+" pnt_plus_2 1.5 0.) (textdisplay "=" pnt_equal 1.5 0.) ) ;;; ;;;SUM_TRIANGLE ;;; (defun sum_triangle() (setq pnt_base (shift_pnt pnt_right (list (+ 2. (* 0.5 base_length)) -2.)) pnt_chr (shift_pnt pnt_base (list -0.20 -0.30)) pnt_cr31 (shift_pnt pnt_base (list 1. 1.)) ) (make_circle_1 "0" 0 pnt_base 0.5) ;;draw a number on this disc (textdisplay (itoa (+ nstep 2)) pnt_chr 0.6 0.) (setq ss4 (ssget "C" pnt_cr31 pnt_chr)) ;;;copy this circle with sum in the triangular pattern ;;Circles will be drawn down to nstep-th level. ;;number of layers starts from 2-nd ;;number of circles to be drawn on each layer is n ;;for the n-th layer. (setq n_layer (1- nstep) n_th 2 ) ;;;outer loop (repeat n_layer (setq n_space (1- n_th) h_down (* space_down n_space) pt_left (shift_pnt pnt_base (list (* co_tang h_down) h_down)) pt_right (shift_pnt pnt_base (list (- (* co_tang h_down)) h_down)) base_length (distance pt_right pt_left) ball_space (/ base_length n_space) nth_ball 1 npos 0 ) (repeat n_th (setq pt_ball (shift_pnt pt_left (list (* npos ball_space) 0.))) (command "_.copy" ss4 "" pnt_base pt_ball) (setq npos (1+ npos)) );;;inner repeat loop (setq n_th (1+ n_th)) );;;outer repeat loop ;;; ) ;;; ;;; ;;;FINAL_TITLE ;;; (defun final_title() (textdisplay "3 x Tet = Tri x (n+2)" '(-0.6688 1.7869) 1.5726 0.) (textdisplay "n" '(6.4319 1.2267 ) 0.8225 0.) (textdisplay "n" '(13.0467 1.3014 ) 0.8225 0.) ) ;;; ;;; ;;;draw_boundary (defun draw_boundary( ) (setq p0 (shift_pnt pnt_org '(0 1.35)) p1 (shift_pnt pnt_org (list (- (* space_left nstep) 0.75) (+ (* space_down nstep) 0.75)) ) p2 (shift_pnt pnt_org (list (+ (* space_right nstep) 0.75) (+ (* space_down nstep) 0.75)) ) p3 (shift_pnt pnt_org (list 0. (+ (* space_down nstep) 0.75))) p1_down (shift_pnt p1 (list 0. (- (* space_down nstep) 1.) )) p2_down (shift_pnt p2 (list 0. (- (* space_down nstep) 1.)) ) p3_down (shift_pnt p3 (list 0. -0.25)) ) (command "_.pline" p0 "_width" 0.10 "" p1 p2 "cl") (command "_.pline" p1 "_width" 0.10 "" p1_down p3_down p3 "cl") (command "_.pline" p2 "_width" 0.10 "" p2_down p3_down p3 "cl") ) ;;; ;;;text_arrow (defun text_arrow( / mid_p01 mid_p1d3d mid_p2d3d pnt_a pnt_b pnt_c pa_arrow pb_arrow pc_arrow) (setq mid_p01 (mid_point p0 p1) mid_p1d3d (mid_point p1_down p3_down) mid_p2d3d (mid_point p2_down p3_down) pnt_a (shift_pnt mid_p01 '(-1.0 0.5)) pnt_b (shift_pnt mid_p1d3d '(0.25 -1.5)) pnt_c (shift_pnt mid_p2d3d '(-1.0 -1.5)) pa_arrow (shift_pnt pnt_a '(-0.5 1.85)) pb_arrow (shift_pnt pnt_b '(1.0 -1.0)) pc_arrow (shift_pnt pnt_c '(-1.0 1.0)) ) (textdisplay "A" pnt_a 1.25 0.) (textdisplay "B" pnt_b 1.25 0.) (textdisplay "C" pnt_c 1.25 0.) (command "_.insert" "white_arrow" pa_arrow 2.0 2.0 0.0) (command "_.insert" "white_arrow" pb_arrow 2.0 2.0 45.) (command "_.insert" "white_arrow" pc_arrow 2.0 2.0 -45.) ) ;;; ;;; (defun setup_sumsqr3 ( ) (setup_sysvar) (setvar "PDMODE" 32) (setvar "PDSIZE" -3) (if (/= (getvar "UNDOCTL") 0) (command "_.undo" "_C" "_N") ) ;no und (command "shademode" "Gouraud") (setq space_down -1.5 space_right 1.5 space_left -1.5 ref_len 15. co_tang (/ 1. (sqrt 3.)) lower_left '(-5.6823 -14.1616) upper_right '( 28.5357 2.6042 ) ) (setq pnt_org '(0 0 ) pnt_c1 (shift_pnt pnt_org '(0.1 0)) pnt_1_left (shift_pnt pnt_org (list space_left space_down)) pnt_1_right (shift_pnt pnt_org (list space_right space_down)) pnt_2 (shift_pnt pnt_org (list 0. space_down)) ) (command "_.zoom" "W" lower_left upper_right) (my_block_insert "red_1_ring" pnt_org 0.5) ;;initialize selection set SS1, SS2, SS3 (setq ss1 (ssadd) ss2 (ssadd) ss3 (ssadd) ss4 (ssadd) ss1a (ssadd)) (command "_.regen") (make_jpg) ) ;;;SETUP_sumsqr3 ;;; ;;;n_balls create N different colored balls ;;; (defun n_balls (num / pnt_base col_id ring_name base_ring) (setq n_space (1- num) h_down (* space_down n_space) pnt_left (shift_pnt pnt_org (list (* co_tang h_down) h_down)) pnt_right (shift_pnt pnt_org (list (- (* co_tang h_down)) h_down)) base_length (distance pnt_right pnt_left) ball_space (/ base_length n_space) nth_ball 1 npos 0 ) ;;;draw num balls from 1 to num numbers marked on them (repeat num ;what color and number on the ball ? (setq col_id (rem nth_ball 7) ring_name (cond ((= col_id 0) "white_7_ring") ((= col_id 1) "red_1_ring") ((= col_id 2) "yellow_2_ring") ((= col_id 3) "green_3_ring") ((= col_id 4) "cyan_4_ring") ((= col_id 5) "blue_5_ring") ((= col_id 6) "magenta_6_ring") (t "white_7_ring") ) ) ;position of the ball (setq ball_pos (shift_pnt pnt_left (list (* npos ball_space) 0.))) ;draw one ring (my_block_insert ring_name ball_pos 0.5) ;(make_jpg) ;increment nth_ball, npos (setq nth_ball (1+ nth_ball) npos (1+ npos)) ) ;;;end of repeat (make_jpg) ) ;;;N_balls ;;; (defun c:square_pyramid () (setup_ball) (setq nstep 2) (while (= (Yes_or_No "Go to next step ?") "_Y") (nxn_ball nstep) (IF (= JPG_YES 1) (make_jpg) ) (setq nstep (1+ nstep)) ) ;;;end of while loop (reset_sysvar) ) ;;;C:square_pyramid ;;; ;;; (defun setup_ball () (setup_sysvar) ;(setvar "PDMODE" 32) ;(setvar "PDSIZE" -3) (if (/= (getvar "UNDOCTL") 0) (command "_.undo" "_C" "_N") ) ;no und (command "shademode" "Gouraud") (setq ref_len 15.) (setq z_up '(0 0 2) z_down '(0 0 -5) x_left '(-10 0 0) x_right '(10 0 0) y_far '(0 10 0) y_near '(0 -10 0) ) (make_pt "0" 0 z_up) (make_pt "0" 0 z_down) (make_pt "0" 0 x_left) (make_pt "0" 0 x_right) (make_pt "0" 0 y_far) (make_pt "0" 0 y_near) (command "_.vpoint" "-18.3967,-26.1979,22.8342") ;;SW isometric view (my_block_insert "white_ball" (list -1 -1 (- ref_len 2)) 1.0 ) (setq ref_ball1 (entlast)) ;;this will be used as a reference later (command "_.zoom" "_EXTENT") (command "_.regen") (IF (= JPG_YES 1) (make_jpg) ) ) ;;;SETUP_ball ;;; ;;; ;;;nxn_ball create N by N cannon-ball pyramid using array command ;;; (defun nxn_ball (num / pnt_ins col_id ball_name base_ball) (setq pnt_ins (list (- num) (- num) (- ref_len (* num 2.)))) (setq col_id (rem num 7) ball_name (cond ((= col_id 0) "white_ball") ((= col_id 1) "red_ball") ((= col_id 2) "yellow_ball") ((= col_id 3) "green_ball") ((= col_id 4) "cyan_ball") ((= col_id 5) "blue_ball") ((= col_id 6) "magenta_ball") (t "white_ball") ) ) (princ pnt_ins) (terpri) (my_block_insert ball_name pnt_ins 1.0) (setq base_ball (entlast)) ;;;use ARRAY command to make N x N (command "_.array" base_ball "" "_R" num num 2.0 2.0) ) ;;;NXN_ball ;;;************************************************************************ ;;; Function: PLT_3D ;;; ;;; get a point off the line segment defined by two end points and angle. ;;; ; utility routine ;; get a point along the line segment defined by two end points ;; ;; pnt_a pnt_b end points ;; loc_from_a ratio of distance from a to length AB ;; output point data (x,y) (defun PLT_3D (pnt_a pnt_b loc_from_a / xa xb ya yb za zb dist_ab dx dy dz xm ym zm ) (setq xa (car pnt_a) ya (cadr pnt_a) za (caddr pnt_a) xb (car pnt_b) yb (cadr pnt_b) zb (caddr pnt_b) dx (- xb xa) dy (- yb ya) dz (- zb za) xm (+ xa (* loc_from_a dx)) ym (+ ya (* loc_from_a dy)) zm (+ za (* loc_from_a dz)) ) (list xm ym zm) ) ;PLT_3D ;;;************************************************************************ ;;; Function: PLD_3d ;;; ;;; get a point off the line segment defined by two end points and angle. ;;; ; utility routine ;; get a point along the line segment defined by two end points ;; ;; pnt_a pnt_b end points ;; dist_from_a distance from a to b ;; ;; output point data (x,y) ;; (defun PLD_3D (pnt_a pnt_b dist_from_a / xa xb ya yb za zb dx dy dz xm ym zm loc_from_a ) (setq xa (car pnt_a) ya (cadr pnt_a) za (caddr pnt_a) xb (car pnt_b) yb (cadr pnt_b) zb (caddr pnt_b) dx (- xb xa) dy (- yb ya) dz (- zb za) loc_from_a (/ dist_from_a (distance pnt_a pnt_b)) xm (+ xa (* loc_from_a dx)) ym (+ ya (* loc_from_a dy)) zm (+ za (* loc_from_a dz)) ) ;(princ zm) (list xm ym zm) ) ;PLD_3D ;;; ;;;******************************************************************************************* ;;; move_ent_3d move a sinlge entity slowly ;;; (defun move_ent_3d (move_entity pnt_start pnt_end n_speed ndiv ncolor layer_name / nstep pnt_from pnt_to inc ) (setq inc (/ 1. ndiv) nstep 1 pnt_from pnt_start pnt_to (plt_3d pnt_start pnt_end inc) ) (repeat ndiv (slow_line_3d n_speed layer_name ncolor pnt_from pnt_to) (entdel (entlast)) ;;;test delete the trace (command "_.move" move_entity "" pnt_from pnt_to) (setq nstep (1+ nstep) pnt_from pnt_to pnt_to (plt_3d pnt_start pnt_end (* inc nstep)) ) ) ) ;;;move_ent_3d ;;; ;;; Function: SLOW_LINE_3D ;;; ;;;draw a line slowly between two given points ;;; (defun SLOW_LINE_3D (n_section layer_name color_code pnt_begin pnt_end / n_section nstep x0 x1 delta_x y0 y1 delta_y z0 z1 delta_z pnt_cur x_next y_next z_next pnt_next ) (setq sblip (getvar "blipmode")) (setq scmde (getvar "cmdecho")) (setvar "blipmode" 0) (setvar "cmdecho" 0) (setq nstep 1) (setq x0 (car pnt_begin) x1 (car pnt_end) y0 (cadr pnt_begin) y1 (cadr pnt_end) z0 (caddr pnt_begin) z1 (caddr pnt_end) delta_x (/ (- x1 x0) (float n_section)) delta_y (/ (- y1 y0) (float n_section)) delta_z (/ (- z1 z0) (float n_section)) ) (setq pnt_cur pnt_begin) (while (< nstep n_section) (progn (setq x_next (+ x0 (* delta_x nstep)) y_next (+ y0 (* delta_y nstep)) z_next (+ z0 (* delta_z nstep)) pnt_next (list x_next y_next z_next) ) (command "_.line" pnt_begin pnt_next "") (entdel (entlast)) ) (setq nstep (+ 1 nstep)) ) (make_line_1 layer_name color_code pnt_begin pnt_end) ;slow displayed line (setvar "blipmode" sblip) (setvar "cmdecho" scmde) ) ;SLOW_LINE_3d ;;;************************************************************************ ;;;SLOW_MOVE ;;; (defun slow_move(move_object pnt_from pnt_to nstep time_delay / del_inc nloop from to) (if (= jpg_yes 1) (setq nstep 3)) (setq del_inc (/ 1. nstep) nloop 0 ) (repeat nstep (setq from (plt pnt_from pnt_to (* nloop del_inc)) to (plt pnt_from pnt_to (* (1+ nloop) del_inc)) ) (command "_.move" move_object "" from to) (make_jpg) (command "_.delay" time_delay) (setq nloop (1+ nloop)) ) ) ;;;SLOW_MOVE ;;; ;;; ;;;SLOW_ROTATE ;;; (defun slow_rotate(move_object pivot rot_angle nstep time_delay / deg_inc nloop ) (if (= jpg_yes 1) (setq nstep 3)) (setq deg_inc (/ rot_angle (float nstep)) nloop 1 ) (repeat nstep (command "_.rotate" move_object "" pivot deg_inc) (make_jpg) (command "_.delay" time_delay) (setq nloop (1+ nloop)) ) ) ;;;SLOW_ROTATE ;;;