DDXXXXBRUH4UHHPOLAK2IG45C4QAPGSR4MTLV3MVFV3EMND43DIAC IHHNPJ5QIEYWJTC34GSVWJ7KSHAEO7RQCQULTA7MA5RUMQ2GVOZQC VHQCNMARPMNBSIUFLJG7HVK4QGDNPCGNVFLHS3I4IGNVSV5MRLYQC OTIBCAUJ3KDQJLVDN3A536DLZGNRYMGJLORZVR3WLCGXGO6UGO6AC UN7L3DNN4Y3BCIA7SM6GYYWQG3OKVCH7ADPKAVF33E6QXY2HEJMQC QYIFOHW3WDDQMK4ATY6IOSQRFHJOQ5QCPDKRC4GVGWLQEH4HGWVQC CRYGI3LRKVHD4D76Q2VTNSX4B2JP5FPDEFJU6DBO7QWFHER2E4OQC OWK3U6VDHPZM6Q6MB7PVFZPDD2KD5NJ3WHVMGTQ2XGUNYDNWSPWAC K2X6G75Z6XBC4DVIRWC5HC7XA3A2SKOM3MWSQTCFEYWIJL7LME2QC NYQ7HD4D5L44UORK52TH7CAEXYN5CE4ZUVLCWMY6XXPYHXVBTGHAC BLWAYPKV3MLDZ4ALXLUJ25AIR6PCIL4RFYNRYLB26GFVC2KQBYBAC W7JMT5V2ELDFZG354LUL5AR6IQLIYQUDXBEPDNZ2BEBXDCZLQJTAC WTDKUACNTWB4KD34TZZNPILNX4FQ6MR64XYBAA5GOMICF73WLIAAC HYEAFRZ2UEKDYTAE2GDQLHEJBPQASP2NDLMXB7F6MTVK2BKOXKEAC 6DE7RBZ6RHNEICJ7EUMCTROK43LW4LYINULIF2QEQOKCXWLUYUXAC SRVDX4I5QKWAH3Y5DX25PG34U7NY55H46ZYG2APH47BUZT3EJ2HAC XNFTJHC4QSHNSIWNN7K6QZEZ37GTQYKHS4EPNSVPQCUSWREROGIQC DRFE3B3ZKRG4RY2R5Q3SDFD3LH4EXUX3CZCDFBNAXVI2SLDS57PAC elseif shape.mode == 'polygon' thenlocal name = shape.center.nameshape.center = Drawing.insert_point(drawing.points, shape.center.x,shape.center.y)drawing.points[shape.center].name = namelocal name = shape.p1.nameshape.p1 = Drawing.insert_point(drawing.points, shape.p1.x,shape.p1.y)drawing.points[shape.p1].name = name
elseif shape.mode == 'polygon' thenlocal name = shape.center.nameshape.center = Drawing.insert_point(drawing.points, shape.center.x,shape.center.y)drawing.points[shape.center].name = namelocal name = shape.p1.nameshape.p1 = Drawing.insert_point(drawing.points, shape.p1.x,shape.p1.y)drawing.points[shape.p1].name = name
endfunction test_draw_polygon()io.write('\ntest_draw_polygon')-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)App.screen.init{width=Margin_left+300, height=300}Lines = load_array{'```lines', '```', ''}Line_width = 256 -- drawing coordinates 1:1 with pixelsApp.draw()check_eq(Current_drawing_mode, 'line', 'F - test_draw_polygon/baseline/drawing_mode')check_eq(#Lines, 2, 'F - test_draw_polygon/baseline/#lines')check_eq(Lines[1].mode, 'drawing', 'F - test_draw_polygon/baseline/mode')check_eq(Lines[1].y, Margin_top+Drawing_padding_top, 'F - test_draw_polygon/baseline/y')check_eq(Lines[1].h, 128, 'F - test_draw_polygon/baseline/y')check_eq(#Lines[1].shapes, 0, 'F - test_draw_polygon/baseline/#shapes')-- start a strokeApp.run_after_mouse_press(Margin_left+35, Margin_top+Drawing_padding_top+36, 1)-- switch to polygon modeApp.run_after_keychord('5')App.run_after_mouse_release(Margin_left+55, Margin_top+Drawing_padding_top+26, 1)local drawing = Lines[1]check_eq(#drawing.shapes, 1, 'F - test_draw_polygon/#shapes')check_eq(#drawing.points, 2, 'F - test_draw_polygon/vertices')local shape = drawing.shapes[1]check_eq(shape.mode, 'polygon', 'F - test_draw_polygon/shape_mode')check_eq(shape.num_vertices, 5, 'F - test_draw_polygon/vertices')
elseif shape.mode == 'polygon' thenlocal center = drawing.points[shape.center]local cx,cy = Drawing.pixels(center.x)+left, Drawing.pixels(center.y)+toplocal p1 = drawing.points[shape.p1]local x,y = Drawing.pixels(p1.x)+left, Drawing.pixels(p1.y)+topDrawing.draw_polygon(shape.num_vertices, cx,cy, x,y)
elseif shape.mode == 'polygon' thenlocal center = drawing.points[shape.center]local mx,my = Drawing.coord(App.mouse_x()-left), Drawing.coord(App.mouse_y()-top)if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h thenreturnendlocal cx,cy = Drawing.pixels(center.x)+left, Drawing.pixels(center.y)+topDrawing.draw_polygon(Current_drawing_submode, cx,cy, App.mouse_x(),App.mouse_y())
elseif chord == 'C-3' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 3elseif chord == 'C-4' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 4elseif chord == 'C-5' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 5elseif chord == 'C-6' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 6elseif chord == 'C-7' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 7elseif chord == 'C-8' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 8elseif chord == 'C-9' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 9elseif chord == '3' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 3Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '4' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 4Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '5' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 5Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '6' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 6Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '7' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 7Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '8' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 8Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '9' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 9Drawing.switch_current_drawing_pending_to_polygon()
function Drawing.switch_current_drawing_pending_to_polygon()local _,drawing = Drawing.current_drawing()if drawing.pending.mode == 'freehand' thendrawing.pending.center = Drawing.insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y)elseif drawing.pending.mode == 'line' or drawing.pending.mode == 'manhattan' thendrawing.pending.center = drawing.pending.p1elseif drawing.pending.mode == 'rectangle' thendrawing.pending.center = drawing.pending.vertices[1]elseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' then-- reuse centerenddrawing.pending.mode = 'polygon'drawing.pending.num_vertices = Current_drawing_submodeend