rectangle and square shapes

[?]
May 18, 2022, 10:29 PM
WTDKUACNTWB4KD34TZZNPILNX4FQ6MR64XYBAA5GOMICF73WLIAAC

Dependencies

  • [2] RT6EV6OP delegate update events to drawings
  • [3] NYQ7HD4D move
  • [4] XNFTJHC4 split keyboard handling between Text and Drawing
  • [5] HYEAFRZ2 split mouse_pressed events between Text and Drawing
  • [6] VHQCNMAR several more modules
  • [7] XRLJDW3W casting about for more helpers to extract..
  • [8] 6DE7RBZ6 move mouse_released events to Drawing
  • [9] PTT4K4EU use the provided args everywhere
  • [*] BLWAYPKV extract a module

Change contents

  • edit in icons.lua at line 41
    [4.6505]
    [4.6505]
    end
    function icon.rectangle(x, y)
    love.graphics.line(x+4,y+8, x+4,y+16)
    love.graphics.line(x+4,y+16, x+16,y+16)
    love.graphics.line(x+16,y+16, x+16,y+8)
    love.graphics.line(x+16,y+8, x+4,y+8)
  • edit in icons.lua at line 50
    [4.6510]
    [4.6510]
    function icon.square(x, y)
    love.graphics.line(x+6,y+6, x+6,y+16)
    love.graphics.line(x+6,y+16, x+16,y+16)
    love.graphics.line(x+16,y+16, x+16,y+6)
    love.graphics.line(x+16,y+6, x+6,y+6)
    end
  • edit in help.lua at line 33
    [4.8097]
    [4.8097]
    y = y+15*Zoom
    end
    if Current_drawing_mode ~= 'rectangle' then
    love.graphics.print("* Press 'ctrl+r' to switch to drawing rectangles", 16+30,y, 0, Zoom)
    y = y+15*Zoom
    end
    if Current_drawing_mode ~= 'square' then
    love.graphics.print("* Press 'ctrl+s' to switch to drawing squares", 16+30,y, 0, Zoom)
  • edit in help.lua at line 99
    [4.10735]
    [4.10735]
    y = y+15*Zoom
    end
    if Current_drawing_mode ~= 'rectangle' then
    love.graphics.print("* Press 'g' to switch to drawing rectangles", 16+30,y, 0, Zoom)
    y = y+15*Zoom
    end
    if Current_drawing_mode ~= 'square' then
    love.graphics.print("* Press 'g' to switch to drawing squares", 16+30,y, 0, Zoom)
  • replacement in geom.lua at line 10
    [4.11923][4.11923:11961]()
    elseif shape.mode == 'polygon' then
    [4.11923]
    [4.11961]
    elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' then
  • edit in geom.lua at line 83
    [4.13814]
    [4.13814]
    end
    -- are (x3,y3) and (x4,y4) on the same side of the line between (x1,y1) and (x2,y2)
    function geom.same_side(x1,y1, x2,y2, x3,y3, x4,y4)
    if x1 == x2 then
    return math.sign(x3-x1) == math.sign(x4-x1)
    end
    if y1 == y2 then
    return math.sign(y3-y1) == math.sign(y4-y1)
    end
    local m = (y2-y1)/(x2-x1)
    return math.sign(m*(x3-x1) + y1-y3) == math.sign(m*(x4-x1) + y1-y4)
    end
    function math.sign(x)
    if x > 0 then
    return 1
    elseif x == 0 then
    return 0
    elseif x < 0 then
    return -1
    end
  • replacement in drawing.lua at line 66
    [3.636][3.636:674]()
    elseif shape.mode == 'polygon' then
    [3.636]
    [3.674]
    elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' then
  • edit in drawing.lua at line 85
    [3.1680]
    [3.1680]
    -- ignore
  • replacement in drawing.lua at line 94
    [3.1825][3.1825:1860]()
    if shape.mode == 'freehand' then
    [3.1825]
    [3.1860]
    if shape.mode == nil then
    -- nothing pending
    elseif shape.mode == 'freehand' then
  • edit in drawing.lua at line 127
    [3.3372]
    [3.3372]
    elseif shape.mode == 'rectangle' then
    local pmx,pmy = love.mouse.getX(), love.mouse.getY()
    local first = drawing.points[shape.vertices[1]]
    if #shape.vertices == 1 then
    love.graphics.line(Drawing.pixels(first.x)+left,Drawing.pixels(first.y)+top, pmx,pmy)
    return
    end
    local second = drawing.points[shape.vertices[2]]
    local mx,my = Drawing.coord(pmx-left), Drawing.coord(pmy-top)
    local thirdx,thirdy, fourthx,fourthy = Drawing.complete_rectangle(first.x,first.y, second.x,second.y, mx,my)
    love.graphics.line(Drawing.pixels(first.x)+left,Drawing.pixels(first.y)+top, Drawing.pixels(second.x)+left,Drawing.pixels(second.y)+top)
    love.graphics.line(Drawing.pixels(second.x)+left,Drawing.pixels(second.y)+top, Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top)
    love.graphics.line(Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top, Drawing.pixels(fourthx)+left,Drawing.pixels(fourthy)+top)
    love.graphics.line(Drawing.pixels(fourthx)+left,Drawing.pixels(fourthy)+top, Drawing.pixels(first.x)+left,Drawing.pixels(first.y)+top)
    elseif shape.mode == 'square' then
    local pmx,pmy = love.mouse.getX(), love.mouse.getY()
    local first = drawing.points[shape.vertices[1]]
    if #shape.vertices == 1 then
    love.graphics.line(Drawing.pixels(first.x)+left,Drawing.pixels(first.y)+top, pmx,pmy)
    return
    end
    local second = drawing.points[shape.vertices[2]]
    local mx,my = Drawing.coord(pmx-left), Drawing.coord(pmy-top)
    local thirdx,thirdy, fourthx,fourthy = Drawing.complete_square(first.x,first.y, second.x,second.y, mx,my)
    love.graphics.line(Drawing.pixels(first.x)+left,Drawing.pixels(first.y)+top, Drawing.pixels(second.x)+left,Drawing.pixels(second.y)+top)
    love.graphics.line(Drawing.pixels(second.x)+left,Drawing.pixels(second.y)+top, Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top)
    love.graphics.line(Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top, Drawing.pixels(fourthx)+left,Drawing.pixels(fourthy)+top)
    love.graphics.line(Drawing.pixels(fourthx)+left,Drawing.pixels(fourthy)+top, Drawing.pixels(first.x)+left,Drawing.pixels(first.y)+top)
  • edit in drawing.lua at line 172
    [3.4352]
    [3.4352]
    else
    print(shape.mode)
    assert(false)
  • replacement in drawing.lua at line 189
    [4.1252][4.1252:1300]()
    elseif Current_drawing_mode == 'polygon' then
    [4.1252]
    [4.1300]
    elseif Current_drawing_mode == 'polygon' or Current_drawing_mode == 'rectangle' or Current_drawing_mode == 'square' then
  • edit in drawing.lua at line 195
    [4.1671]
    [4.1671]
    else
    print(Current_drawing_mode)
    assert(false)
  • edit in drawing.lua at line 224
    [2.1041]
    [2.1041]
    else
    print(Current_drawing_mode)
    assert(false)
  • replacement in drawing.lua at line 264
    [4.1904][4.1904:1974]()
    local j = Drawing.insert_point(Lines.current.points, mx,my)
    [4.1904]
    [4.1974]
    table.insert(Lines.current.pending.vertices, Drawing.insert_point(Lines.current.points, mx,my))
  • replacement in drawing.lua at line 267
    [4.2054][4.2054:2120]()
    table.insert(Lines.current.shapes, Lines.current.pending)
    [4.2054]
    [4.2120]
    elseif Lines.current.pending.mode == 'rectangle' then
    assert(#Lines.current.pending.vertices <= 2)
    if #Lines.current.pending.vertices == 2 then
    local mx,my = Drawing.coord(x-16), Drawing.coord(y-Lines.current.y)
    if mx >= 0 and mx < 256 and my >= 0 and my < Lines.current.h then
    local first = Lines.current.points[Lines.current.pending.vertices[1]]
    local second = Lines.current.points[Lines.current.pending.vertices[2]]
    local thirdx,thirdy, fourthx,fourthy = Drawing.complete_rectangle(first.x,first.y, second.x,second.y, mx,my)
    table.insert(Lines.current.pending.vertices, Drawing.insert_point(Lines.current.points, thirdx,thirdy))
    table.insert(Lines.current.pending.vertices, Drawing.insert_point(Lines.current.points, fourthx,fourthy))
    table.insert(Lines.current.shapes, Lines.current.pending)
    end
    else
    -- too few points; draw nothing
    end
    elseif Lines.current.pending.mode == 'square' then
    assert(#Lines.current.pending.vertices <= 2)
    if #Lines.current.pending.vertices == 2 then
    local mx,my = Drawing.coord(x-16), Drawing.coord(y-Lines.current.y)
    if mx >= 0 and mx < 256 and my >= 0 and my < Lines.current.h then
    local first = Lines.current.points[Lines.current.pending.vertices[1]]
    local second = Lines.current.points[Lines.current.pending.vertices[2]]
    local thirdx,thirdy, fourthx,fourthy = Drawing.complete_square(first.x,first.y, second.x,second.y, mx,my)
    table.insert(Lines.current.pending.vertices, Drawing.insert_point(Lines.current.points, thirdx,thirdy))
    table.insert(Lines.current.pending.vertices, Drawing.insert_point(Lines.current.points, fourthx,fourthy))
    table.insert(Lines.current.shapes, Lines.current.pending)
    end
    end
  • edit in drawing.lua at line 309
    [4.3044]
    [4.3044]
    else
    print(Lines.current.pending.mode)
    assert(false)
  • replacement in drawing.lua at line 350
    [4.6652][4.6652:6744]()
    elseif love.mouse.isDown('1') and chord == 'p' and Current_drawing_mode == 'polygon' then
    [4.6652]
    [4.6744]
    elseif chord == 'C-r' and not love.mouse.isDown('1') then
    Current_drawing_mode = 'rectangle'
    elseif love.mouse.isDown('1') and chord == 'r' then
    Current_drawing_mode = 'rectangle'
    local drawing = Drawing.current_drawing()
    if drawing.pending.mode == 'freehand' then
    drawing.pending.vertices = {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' then
    if drawing.pending.vertices == nil then
    drawing.pending.vertices = {drawing.pending.p1}
    end
    elseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' then
    drawing.pending.vertices = {drawing.pending.center}
    end
    drawing.pending.mode = 'rectangle'
    elseif chord == 'C-s' and not love.mouse.isDown('1') then
    Current_drawing_mode = 'square'
    elseif love.mouse.isDown('1') and chord == 's' then
    Current_drawing_mode = 'square'
    local drawing = Drawing.current_drawing()
    if drawing.pending.mode == 'freehand' then
    drawing.pending.vertices = {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' then
    if drawing.pending.vertices == nil then
    drawing.pending.vertices = {drawing.pending.p1}
    end
    elseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' then
    drawing.pending.vertices = {drawing.pending.center}
    end
    drawing.pending.mode = 'square'
    elseif love.mouse.isDown('1') and chord == 'p' and (Current_drawing_mode == 'polygon' or Current_drawing_mode == 'rectangle' or Current_drawing_mode == 'square') then
  • edit in drawing.lua at line 497
    [4.11786]
    [4.11786]
    end
    end
    function Drawing.complete_rectangle(firstx,firsty, secondx,secondy, x,y)
    if firstx == secondx then
    local thirdx = y
    local thirdy = secondy
    love.graphics.line(Drawing.pixels(secondx)+left,Drawing.pixels(secondy)+top, Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top)
    love.graphics.line(Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top, Drawing.pixels(thirdx)+left,Drawing.pixels(firsty)+top)
    return thirdx,thirdy, thirdx,firsty
    end
    if firsty == secondy then
    local thirdx = secondx
    local thirdy = y
    love.graphics.line(Drawing.pixels(secondx)+left,Drawing.pixels(secondy)+top, Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top)
    love.graphics.line(Drawing.pixels(thirdx)+left,Drawing.pixels(thirdy)+top, Drawing.pixels(firstx)+left,Drawing.pixels(thirdy)+top)
    return
    end
    local first_slope = (secondy-firsty)/(secondx-firstx)
    -- slope of second edge:
    -- -1/first_slope
    -- equation of line containing the second edge:
    -- y-secondy = -1/first_slope*(x-secondx)
    -- => 1/first_slope*x + y + (- secondy - secondx/first_slope) = 0
    -- now we want to find the point on this line that's closest to the mouse pointer.
    -- https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_an_equation
    local a = 1/first_slope
    local c = -secondy - secondx/first_slope
    local thirdx = ((x-a*y) - a*c) / (a*a + 1)
    local thirdy = (a*(-x + a*y) - c) / (a*a + 1)
    -- slope of third edge = first_slope
    -- equation of line containing third edge:
    -- y - thirdy = first_slope*(x-thirdx)
    -- => -first_slope*x + y + (-thirdy + thirdx*first_slope) = 0
    -- now we want to find the point on this line that's closest to the first point
    local a = -first_slope
    local c = -thirdy + thirdx*first_slope
    local fourthx = ((firstx-a*firsty) - a*c) / (a*a + 1)
    local fourthy = (a*(-firstx + a*firsty) - c) / (a*a + 1)
    return thirdx,thirdy, fourthx,fourthy
    end
    function Drawing.complete_square(firstx,firsty, secondx,secondy, x,y)
    -- use x,y only to decide which side of the first edge to complete the square on
    local deltax = secondx-firstx
    local deltay = secondy-firsty
    local thirdx = secondx+deltay
    local thirdy = secondy-deltax
    if not geom.same_side(firstx,firsty, secondx,secondy, thirdx,thirdy, x,y) then
    deltax = -deltax
    deltay = -deltay
    thirdx = secondx+deltay
    thirdy = secondy-deltax
  • edit in drawing.lua at line 551
    [4.11792]
    [4.11792]
    local fourthx = firstx+deltay
    local fourthy = firsty-deltax
    return thirdx,thirdy, fourthx,fourthy