Search code examples
leafletgeometrygeospatialturfjs

create leaflet rectangle from given 2 diagonal points


i have a leaflet feature rectangle and i need to pragmatically draw another squre rectangle . see the image dotted rectangle is the one i need to calculate

possible solutions

  1. get center of rectangle create new 4 point from center draw a rectangle i there any other better solutions for this

enter image description here


Solution

  • Distance from square vertices to rectangle center is

    d = rect_width / 2 + rect_height / 2
    

    So their coordinates are

    rect_center_x + d; rect_center_y  
    rect_center_x; rect_center_y - d  
    rect_center_x - d; rect_center_y  
    rect_center_x; rect_center_y + d