Quote:

> I am wondering if anyone can help me.

> I am writing a program that models business processes. It has an interface

> similar to Rational Rose (Visual Modeler). I would like to be able to draw

> a connection between two objects.

[ ... ]

Quote:

> The connection should be from the edge of A to the edge of B using the edges

> of the objects closest to each other. All this is based on a straight line

> from the center of A to the center of B. Drawing a line from the center of

> A to the Center of B is no problem. The problem arises from drawing the

> line only to the EDGE of each object.

The suggestion you've already gotten, to draw the lines, then draw the

boxes over them is probably the simplest one, though it'll cause a

little bit of flicker as the box is drawn over the line.

I'd try that out first to find whether it's really bothersome -- my

guess is that it won't be a problem. If you decide it really is a

problem, you can do one of two basic things: you can set a clipping

region while you draw the line, so it won't be drawn except in the

proper area, or you can explicitly figure out the proper end-points

and only draw the line where you really want to.

Hopefully it'll only take a quick refresher in the geometry involved

to get you on the right track -- if you haven't taken a course in

geometry, you may find this hard to follow, but it's a bit much to try

to cover from the very beginning..

The usual formula for a line is y=mx+b. m is the slope and b is the y

intercept. Given a pair of points x1,y1 and x2,y2 (the centers of the

two squares in your case) you can find the slope between them as:

m=(y1-y2)/(x1-x2). Now we have to find b -- this is the value of y

for an x value of 0. Start by assuming that b=0, and figure out a

tentative value of y for x1: y0=m*x1. Now, the difference between y1

and y0 is the y intercept of the line: b = y1-(m*x1)

You don't need to worry much about the slopes for the lines in the

boxes: for horizontal lines it's 0 and for vertical lines it's

infinite. Likewise, a vertical line won't have a y intercept at all,

and a horizontal line will have a y intercept equal to its y value.

This makes life a bit easier to deal with: to find the intersection

between the sloped line and a horizontal line, you only have to figure

out the proper x value -- the y value is always the y value of the

horizontal line.

Likewise, for a vertical line you only have to figure out the y value.

We'll start with this case. Given a sloped line y=mx+b, we just

figure out the y value for the same x value as the vertical line.

Now there's one minor detail left: figuring out whether your sloped

line will intersect the horizontal or vertical boundary of the box.

There are a number of ways to do this, but the simplest is probably to

figure out the slope from the center of the box to each corner of the

box. The connecting line will normally have a slope between the

slopes of two of those lines, and it intersects the line connecting

the two corresponding corners. If it happens to be exactly the same

slope as one of those lines, it means the line will run exactly

through the corner, and you already know its exact end-point

coordinate (the coordinate of that corner). Note that as long as you

use a particular shape of box, you can pre-figure the slopes of these

four lines rather than re-figuring them every time you need to draw a

connecting line.