How can I draw multiple lines connected via “nodes” in winforms c#? -
i'm writing dijkstra algorithm want draw lines between each 2 nodes connect 2 nodes each other writing code draw 1 line between 2 nodes.i wanna each time click on 2 node created line.of course , using "drawlines()"
method create 1 line.
public class graph { public dictionary<int, list<keyvaluepair<int, int>>> vertices = new dictionary<int, list<keyvaluepair<int, int>>>(); public void addvertex(int id, list<keyvaluepair<int, int>> edges) { if (!vertices.containskey(id)) { vertices.add(id, new list<keyvaluepair<int, int>>()); } vertices[id].addrange(edges); } }
private circlemanager circlemanager = new circlemanager(); private list<circle> circlessourceanddestination = new list<circle>(); private graph g = new graph(); private int lastclickednode = -1; public form1() { initializecomponent(); picturebox1.paint += new painteventhandler(pic_paint); } private void pic_paint(object sender, painteventargs e) { e.graphics.smoothingmode = smoothingmode.antialias; graphics g = e.graphics; drawcircles(g); drawlines(g); } /// <summary> /// method specifies neighbors of node /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private int currentnode = 0; private void picturebox1_mouseclick(object sender, mouseeventargs e) { if (e.button != mousebuttons.right) { //name of node in variable result. example : 0 or 1 ,... var result = circlemanager.hittest(e.location); //node doesn't should negative if (result != -1) { circlessourceanddestination.add(circlemanager.circles[result]); if (lastclickednode == -1) { lastclickednode = result; return; } var weigth = calculatelengthsourceanddestination(circlessourceanddestination); circlessourceanddestination.clear(); if (weigth < 0) { weigth *= -1; } int secondnode = result; var neighbor = new keyvaluepair<int, int>(secondnode, weigth); var list = new list<keyvaluepair<int, int>> { neighbor }; g.addvertex(lastclickednode, list); var nodes = g; extractnodes(nodes); //drawlines(points); lastclickednode = -1; } } else { var result = circlemanager.hittest(e.location); if (result != -1) { circlemanager.circles[circlemanager.hittest(e.location)].selected = true; circlemanager.circles[result].selectfillcolor = color.red; } } picturebox1.invalidate(); } private list<point> pnt = new list<point>(); private void extractnodes(graph nodes) { foreach (var item in nodes.vertices) { foreach (var circle in circlemanager.circles) { if (item.key == convert.toint32(circle.name) && pnt.count == 0) { pnt.add(circle.location); } extractnodestwo(item.value); if (pnt.count == 2) { return; } } } } private void extractnodestwo(list<keyvaluepair<int, int>> value) { if (pnt.count != 0) { foreach (var item in circlemanager.circles) { foreach (var val in value) { if (val.key == convert.toint32(item.name)) { pnt.add(item.location); } } if (pnt.count == 2) return; } } } private void drawlines(graphics g) { if (pnt.count == 2) { lineargradientbrush liner = new lineargradientbrush(pnt[0], pnt[1], color.transparent, color.black); pen pen = new pen(liner); g.drawlines(pen, pnt.toarray()); pnt.clear(); } } }
you calling drawlines(graphics g) method once when paint method called. implies want draw lines in method only. but, g.drawlines(pen, sd);
not in loop. so, can draw 1 line when executed.
i think g.drawlines(pen, sd);
should in kind of loop iterates 'n' no of times(where n number of lines want draw).
Comments
Post a Comment