graph - Drawing things in the right order, Java -
i'm in process of writing "paint" application in java consisting of rectangular nodes , undirected edges. problem beginning of edge in center of rectangle edge connected to, , end of edge in center of other rectangle edge connected to.
to avoid drawing intersection between rectangle , edge, draw edges first , nodes afterwards, placed on top of edges, hiding intersection.
the problem arises when node, not correspond edge placed along edge. in scenario, because edges drawn first, node appears on top of edge. however, not want. want edge show on top of node.
images: how is , how should be
the comprehensive way can think of fix draw nodes first , afterwards draw edges avoiding intersections of edge 2 nodes connected to. difficult on paper because draw edges using drawline
, , not have possibility check intersections.
this why draw edges first, problem mentioned arises. ideas?
what need solution problem #1. need "move" edge's endpoints away center of rectangle. way won't have draw nodes last , automatically solve question @ hand.
my proposed solution in step 2 works if connecting nodes left , right of each other, otherwise need more advanced math of made start in step 2.1.
step 1: edge nothing more 2 point
s point
x
, y
value. find left , right ends of edge comparing x
of each edge. we'll save xleft
, xright
, yleft
, yright
simplicity. maybe have else this, idea.
step 2: move xleft
half width of rectangle right , subtract half width of rectangle xright
.
(optional) step 2.1: in case 2 connecting nodes above each other, can use math.atan2(yright-yleft, xright-xleft)
calculate angle. if value in lower spectrum (between -3pi/4
, -pi/4
) add , subtract half height of rectangle on respective y values. can fiddle around create math , logic 4 directions if want.
step 3: draw new edges after having drawn nodes. edges "connected" edge(s) of rectangles.
feel free ask questions, although suggest try putting code first.