;;;Summation of Odd integers ;;;sumodd_1 ;;; Sept 19,2003--Sept 22,2003 ;;; Copyright 2006 Takaya Iwamoto ;;; (defun c:sumodd_1 () (setq nstep 1) (setup_oddsum) (while (= (Yes_or_No "Go to next step ?") "_Y") (setq col_id (rem nstep 2)) ;(draw_dot nstep col_id) (draw_ball nstep col_id) (draw_fence nstep) (draw_text nstep) (IF (= JPG_YES 1) (make_jpg) ) (setq nstep (1+ nstep)) ) ;;;end of while loop (draw_final_text) (IF (= JPG_YES 1) (make_jpg) ) (alert "\n********End*******\nIt is clear that \nsum of odd integers (2n-1) \nis N squared." ) (command "_.zoom" "_EXTENT") (reset_sysvar) );;;SUMODD_1 ;;; (defun setup_oddsum ( / px py pxy lower_left upper_right ) (setup_sysvar) (if (= icad 0) (command "shademode" "G")) ;;;draw the first dot and lines (setq py (list 0 nstep) pxy (list nstep nstep) px (list nstep 0) lower_left '(-2.5 -2.25) upper_right '(11.5 12) dot_rad 0.35 y_text1 -0.5 y_text2 -1.25 y_text3 -2.0 nstep (1+ nstep) ) ;;;draw a dot ;(circle_dot "0" 1 '(0.5 0.5) dot_rad) (my_block_insert "red_ball" '(0.5 0.5) dot_rad) ;;;draw a fence (command "_.pline" py pxy px "") (textdisplay "n" (list -2 y_text1) 0.5 0.) (textdisplay "2n-1" (list -2 y_text2) 0.5 0.) (textdisplay "Sum" (list -2 y_text3) 0.5 0.) (draw_text 1) (command "_.zoom" "W" lower_left upper_right) (IF (= JPG_YES 1) (make_jpg) ) ) ;; ;;; ;;;DRAW_DOT draw dots (2n-1) = (n-1) + (n-1) + 1 ;;; (n-1) each horizontally & vertically at x & y = (n-0.5) ;;; remaining one at x = y = (n-0.5) ;;; (defun draw_dot (num col_id / ns coord x0 y0 delta step x_loc y_loc xy_loc) (setq ns (1- num) coord (- num 0.5) x0 0.5 y0 0.5 xy_loc (list coord coord) delta 1.0 step 0 ) (repeat ns (setq x_loc (list coord (+ y0 (* step delta))) y_loc (list (+ x0 (* step delta)) coord) ) (circle_dot "0" col_id x_loc dot_rad) (circle_dot "0" col_id y_loc dot_rad) (setq step (1+ step)) ) ;;;loop for repeat (circle_dot "0" col_id xy_loc dot_rad) ) ;;;DRAW_DOT ;;; ;;; ;;;DRAW_BALL draw balls (2n-1) = (n-1) + (n-1) + 1 ;;; (n-1) each horizontally & vertically at x & y = (n-0.5) ;;; remaining one at x = y = (n-0.5) ;;; (defun draw_ball (num col_id / ns coord x0 y0 delta step x_loc y_loc xy_loc) (setq ns (1- num) coord (- num 0.5) x0 0.5 y0 0.5 xy_loc (list coord coord) delta 1.0 step 0 ) (setq ball_name (get_ballname col_id)) (repeat ns (setq x_loc (list coord (+ y0 (* step delta))) y_loc (list (+ x0 (* step delta)) coord) ) (my_block_insert ball_name x_loc dot_rad) (my_block_insert ball_name y_loc dot_rad) (setq step (1+ step)) ) ;;;loop for repeat (my_block_insert ball_name xy_loc dot_rad) ) ;;;DRAW_DOT ;;; ;;;GET_BALLNAME ;;; (defun get_ballname (colcode) (cond ((= colcode 0) "white_ball") ((= colcode 1) "red_ball") ((= colcode 5) "blue_ball") (t "white_ball") ) ) ;;;GET_BALLNAME ;;; ;;;DRAW_FENCE ;;; (defun draw_fence (num / px py pxy) ;;;draw the first dot and lines (setq py (list 0 num) pxy (list num num) px (list num 0) ) ;;;draw a fence (command "_.pline" py pxy px "") ) ;;;DRAW_FENCE ;;; ;;; ;;;DRAW_TEXT ;;; (defun draw_text (num / xtext loc1 loc2 loc3 oddnum sum) (setq xtext (- num 0.6) loc1 (list xtext y_text1) loc2 (list xtext y_text2) loc3 (list xtext y_text3) oddnum (1- (* 2 num)) sum (* num num) ) (textdisplay (itoa num) loc1 0.35 0.) (textdisplay (itoa oddnum) loc2 0.35 0.) (textdisplay (itoa sum) loc3 0.35 0.) ) ;;;draw_text ;;; ;;; CIRCLE_DOT colored circle dot display ;;; (defun CIRCLE_DOT (layername color_id center_pt dot_radius) (setq cur_col (getvar "cecolor")) (setvar "cecolor" (itoa color_id)) (make_circle_1 layername color_id center_pt dot_radius) (command "_.hatch" "_solid" (entlast) "") (setvar "cecolor" cur_col) ) ;;;CIRCLE_DOT ;;; ;;;DRAW_FINAL_TEXT ;;; (defun draw_final_text ( / final_text1 final_text2 final_text3 final_loc1 final_loc2 final_loc3 fin_x fin_y pfx pfy pfxr pfyr pfl pflu pfyu ) (setq final_text1 "Sum of Odd integers" final_text2 "from 1 to (2N-1)" final_text3 "is N x N " final_loc1 '(-6 6) final_loc2 '(-6 5) final_loc3 '(-6 4) fin_y (- nstep 1) fin_x fin_y pfx (list fin_x 0) pfy (list fin_x fin_y) pfxr (list (+ 2 fin_x) 0) pfyr (list (+ 2 fin_x) fin_y) pfl (list 0 fin_y) pflu (list 0 (+ 2 fin_y)) pfyu (list fin_x (+ 2 fin_y)) ) (textdisplay final_text1 final_loc1 0.45 0.) (textdisplay final_text2 final_loc2 0.45 0.) (textdisplay final_text3 final_loc3 0.45 0.) (make_line_1 "0" 8 pfy pfyr) (make_line_1 "0" 8 pfx pfxr) (make_line_1 "0" 8 pfxr pfyr) (make_line_1 "0" 8 pfl pflu) (make_line_1 "0" 8 pfy pfyu) (make_line_1 "0" 8 pflu pfyu) (textdisplay "N" (list (* 0.5 fin_x) (+ 2 fin_y)) 0.45 0.) (textdisplay "N" (list (+ 2 fin_x) (* 0.5 fin_y)) 0.45 0.) ) ;;;draw_final_text