Click here to Skip to main content
15,887,683 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: optimizing the set of possible boolean outcomes of evaluating multiple conditions ? [modified] Pin
BillWoodruff19-Aug-11 6:51
professionalBillWoodruff19-Aug-11 6:51 
AnswerRe: optimizing the set of possible boolean outcomes of evaluating multiple conditions ? Pin
GParkings3-Sep-11 5:57
GParkings3-Sep-11 5:57 
Questionconnection routing algorithm for connecting rectangles with lines ? Pin
BillWoodruff2-Aug-11 23:47
professionalBillWoodruff2-Aug-11 23:47 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
David19875-Aug-11 1:21
David19875-Aug-11 1:21 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
BillWoodruff5-Aug-11 2:10
professionalBillWoodruff5-Aug-11 2:10 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
David19875-Aug-11 2:40
David19875-Aug-11 2:40 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
BillWoodruff5-Aug-11 7:29
professionalBillWoodruff5-Aug-11 7:29 
AnswerRe: connection routing algorithm for connecting rectangles with lines ? Pin
cjb1106-Aug-11 9:35
cjb1106-Aug-11 9:35 
For starting, I think you ignore the connection point issue, that's UI, and really a lot of Graph Theory isn't concerned with physical layout. Stick with pointsSmile | :)

Your issue is intersecting paths/edges.

I used Force-Based positioning for mine...which does seem to reduce the number of overlaps, but this is more by fluke rather than design, as no part of the algo cares about the route of the edge.

But maybe you could use it to get a layout, then find any intersects, and 'shove' the lesser connected node out (randomly). That's a first thought, and there is likely to be better math out there! You'd need to tell the force-based algo that some nodes are fixed, most I've seen don't consider that.

Your going to need some cyclic path checking I think as well, as there will be a relation between the number of cyclic paths and the ability to position the nodes with no overlap, more cycles must decrease that ability.

I've got this edge layout:
C#
public static GraphicsPath StraightPath(Point from, Point to, Rectangle fromRect, Rectangle toRect)
{
    GraphicsPath path = new GraphicsPath();
    //if short distance then just add a return a single line path
    if (PointDistance(from, to) < 40)
    {
        path.AddLine(from, to);
        return path;
    }
    List<Point> outputPoints = new List<Point>(6);
    outputPoints.Add(from);
    List<Point> inputPoints = new List<Point>(6);
    inputPoints.Add(to);
    bool isReversed = from.X > to.X;
    //swivel points to they no longer intersect the two rectangles
    int direction = from.Y >= to.Y ? fromRect.Top : fromRect.Bottom;
    if (LineRectangleIntersection(fromRect, outputPoints[outputPoints.Count - 1], inputPoints[inputPoints.Count - 1]))
        outputPoints.Add(new Point(outputPoints[outputPoints.Count - 1].X, direction));
    direction = from.Y <= to.Y ? toRect.Top : toRect.Bottom;
    if (LineRectangleIntersection(toRect, outputPoints[outputPoints.Count - 1], inputPoints[inputPoints.Count - 1]))
        inputPoints.Add(new Point(inputPoints[inputPoints.Count - 1].X, direction));
    //reverse the input points and add to output points
    inputPoints.Reverse();
    outputPoints.AddRange(inputPoints);
    path.AddLines(outputPoints.ToArray());
    return path;
}
public static bool LineRectangleIntersection(Rectangle rect, Point p1, Point p2)
{
    //top
    if (LineSegmentIntersection(p1, p2, new Point(rect.Left, rect.Top), new Point(rect.Right, rect.Top)))
    {
        return true;
    }
    //bottom
    if (LineSegmentIntersection(p1, p2, new Point(rect.Left, rect.Bottom), new Point(rect.Right, rect.Bottom)))
    {
        return true;
    }
    //left
    if (LineSegmentIntersection(p1, p2, new Point(rect.Left, rect.Top), new Point(rect.Left, rect.Bottom)))
    {
        return true;
    }
    //right
    if (LineSegmentIntersection(p1, p2, new Point(rect.Right, rect.Top), new Point(rect.Right, rect.Bottom)))
    {
        return true;
    }
    // no intersection
    return false;
}
internal static bool LineSegmentIntersection(
    float Ax, float Ay,
    float Bx, float By,
    float Cx, float Cy,
    float Dx, float Dy)
{
    float distAB, theCos, theSin, newX, ABpos;
    //  Fail if either line segment is zero-length.
    if (Ax == Bx && Ay == By || Cx == Dx && Cy == Dy)
        return false;
    //  (1) Translate the system so that point A is on the origin.
    Bx -= Ax;
    By -= Ay;
    Cx -= Ax;
    Cy -= Ay;
    Dx -= Ax;
    Dy -= Ay;
    //  Discover the length of segment A-B.
    distAB = (float) Math.Sqrt(Bx * Bx + By * By);
    //  (2) Rotate the system so that point B is on the positive X axis.
    theCos = Bx / distAB;
    theSin = By / distAB;
    newX = Cx * theCos + Cy * theSin;
    Cy = Cy * theCos - Cx * theSin;
    Cx = newX;
    newX = Dx * theCos + Dy * theSin;
    Dy = Dy * theCos - Dx * theSin;
    Dx = newX;
    //  Fail if segment C-D doesn't cross line A-B.
    if (Cy < 0f && Dy < 0f || Cy >= 0f && Dy >= 0f)
        return false;
    //  (3) Discover the position of the intersection point along line A-B.
    ABpos = Dx + (Cx - Dx) * Dy / (Dy - Cy);
    //  Fail if segment C-D crosses line A-B outside of segment A-B.
    if (ABpos < 0f || ABpos > distAB)
        return false;
    //  Success.
    return true;
}

Which for a single node pair and edge will draw the edge without touching either node...it basically juts to outside the bounds of each node then a direct line. Looks nice though.

Forgot to say, I was thinking more of laying out the rectangles so that overlapping edges don't occur...you could just layout the rects any old way, and route the edges using something like an A* algo. But you could end up with very long windy edges...that's one issue I've got at the mo.
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
BillWoodruff7-Aug-11 13:02
professionalBillWoodruff7-Aug-11 13:02 
AnswerRe: connection routing algorithm for connecting rectangles with lines ? Pin
Member 41945937-Aug-11 10:48
Member 41945937-Aug-11 10:48 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? [modified] Pin
BillWoodruff7-Aug-11 14:30
professionalBillWoodruff7-Aug-11 14:30 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
Member 41945937-Aug-11 15:34
Member 41945937-Aug-11 15:34 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
Member 41945937-Aug-11 15:59
Member 41945937-Aug-11 15:59 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
BillWoodruff7-Aug-11 17:40
professionalBillWoodruff7-Aug-11 17:40 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
Member 41945938-Aug-11 17:57
Member 41945938-Aug-11 17:57 
GeneralRe: connection routing algorithm for connecting rectangles with lines ? Pin
BillWoodruff16-Aug-11 18:36
professionalBillWoodruff16-Aug-11 18:36 
QuestionRaytracing render quality [modified] Pin
Thomas.D Williams31-Jul-11 11:48
Thomas.D Williams31-Jul-11 11:48 
AnswerRe: Raytracing render quality Pin
Richard MacCutchan31-Jul-11 22:53
mveRichard MacCutchan31-Jul-11 22:53 
GeneralRe: Raytracing render quality Pin
Thomas.D Williams31-Jul-11 23:44
Thomas.D Williams31-Jul-11 23:44 
GeneralRe: Raytracing render quality Pin
David19875-Aug-11 1:07
David19875-Aug-11 1:07 
GeneralRe: Raytracing render quality Pin
Thomas.D Williams5-Aug-11 5:00
Thomas.D Williams5-Aug-11 5:00 
QuestionUnsigned Integer Mod/Remainder Pin
John Paul Walker28-Jul-11 13:11
John Paul Walker28-Jul-11 13:11 
AnswerRe: Unsigned Integer Mod/Remainder Pin
Dr.Walt Fair, PE28-Jul-11 19:18
professionalDr.Walt Fair, PE28-Jul-11 19:18 
GeneralRe: Unsigned Integer Mod/Remainder Pin
John Paul Walker29-Jul-11 13:40
John Paul Walker29-Jul-11 13:40 
GeneralRe: Unsigned Integer Mod/Remainder Pin
Dr.Walt Fair, PE30-Jul-11 17:23
professionalDr.Walt Fair, PE30-Jul-11 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.