Rectangle connect)

Source: Internet
Author: User
Tags integer division

The problem of rectangular line connection is to establish a line with no overlay twists and turns between two rectangles (the line does not cover the image). My method is as follows:

 

Cpoint PTS [5]; // output the vertex list of The Link

Int NPTS; // Number of point points in the output point list

 

Void getrectconnectlines (cpoint * pts, int NPTS)

{

Crect recta, rectb; // two rectangles

Cpoint AC, BC; // rectangular Center

Crect rectc; // a rectangle with a center link

Int aw = recta. Width ();

Int BW = rectb. Width ();

Int Ah = recta. Height ();

Int bH = rectb. Height ();

AC = recta. centerpoint ();

 BC = rectb. centerpoint ();

Rectc = crect (AC, BC );

Int CW = rectc. Width ();

Int CH = rectc. Height ();

 

// (1) when the rectangle can find the central separator, and the distance is not smaller than a certain limit value (it should not be connected in a small gap ), the rectangle is connected to the middle separator line near the midpoint of the middle separator line.

Int LD = 50; // you can specify the distance between the split wires.

Int TD = 0;

TD = Cw * 2-aw-bw;

If (TD> LD * 2) // If the width of the center-connected rectangle is greater than that of the two rectangles, there is a vertical center split line (using the ** 2 judgment to prevent integer division loss of precision)

{

If (AC. x <BC. X) // the right side of the rectangle and the left side of the B rectangle are connected to the vertical split line.

{

PTS [0] = ac;

PTS [0]. offset (AW/2.f, 0 );

PTS [1] = PTS [0];

PTS [1]. offset (TD/4.f, 0 );

PTS [3] = BC;

PTS [3]. offset (-bw/2.f, 0 );

PTS [2] = PTS [3];

PTS [2]. offset (-TD/4.f, 0 );

PTS [1]. x = PTS [2]. X; // align

}

Else // the left of the rectangle and the right of the B rectangle are connected to the vertical split line

{
PTS [0] = ac;
PTS [0]. offset (-AW/2.f, 0 );
PTS [1] = PTS [0];
PTS [1]. offset (-TD/4.f, 0 );
PTS [3] = BC;
PTS [3]. offset (bw/2.f, 0 );
PTS [2] = PTS [3];
PTS [2]. offset (TD/4.f, 0 );

PTS [1]. x = PTS [2]. X; // align

}

NPTS = 4;

Return;

}

 

// In the same way, locate the horizontal split and connect the edge

TD = CH * 2-ah-bh;

If (TD> LD * 2)

{

If (AC. Y <BC. Y)

{

PTS [0] = ac;

PTS [0]. offset (0, AH/2.f );

PTS [1] = PTS [0];

PTS [1]. offset (0, TD/4.f );

PTS [3] = BC;

PTS [3]. offset (0,-bh/2.f );

PTS [2] = PTS [3];

PTS [2]. offset (0,-TD/4.f );

PTS [1]. Y = PTS [2]. Y;

}

Else

{

PTS [0] = ac;

PTS [0]. offset (0,-Ah/2.f );

PTS [1] = PTS [0];

PTS [1]. offset (0,-TD/4.f );

PTS [3] = BC;

PTS [3]. offset (0, BH/2.f );

PTS [2] = PTS [3];

PTS [2]. offset (0, TD/4.f );

PTS [1]. Y = PTS [2]. Y;

}

NPTS = 4;

Return;

}

 

// (2) if the two rectangles do not have a split line and do not overlap the midpoint of the edges, connect them with the closest vertical edge.

 

If ((Cw * 2 <min (AW, BW) + LD)&&(Ch * 2 <max (ah, BH) + LD ))

{
PTS [0] = ac;
PTS [0]. offset (AW/2, 0 );

PTS [1] = ac;
PTS [1]. offset (DD. X, 0 );

PTS [2] = BC;
PTS [2]. offset (0,-bh/2 );

NPTS = 3;
Return;
}

// (3) in the third case, obtain the smallest external rectangle of the two rectangles, connect the unoverwritten side to the edge of the external rectangle, and preferentially find the connectable edge of the same side.

Rectc. unionrect (recta, rectb); // external rectangle

Rectc. inflaterect (LD/2, LD/2); // extended a little distance

Int EA [4] = {0, 0, 0 };

Int EB [4] = {0, 0, 0 };

 

// Locate the edge that is not covered by the midpoint, and prioritize the edge that can be connected to the same side, that is, (EB [I] & EA [I]); if you do not have the same side, you can connect to the next side (Ea [I] & EB [I <3? I: 0])

If (recta-> left <rectb-> left)

{

EA [0] = 1;

If (BC. Y> recta-> bottom) | (BC. Y <recta-> top ))

{

EB [0] = 1;

}

}

Else

{

EB [0] = 1;

If (AC. Y> rectb-> bottom) | (AC. Y <recta-> top ))

{

EA [0] = 1;

}

}

If (recta-> right> rectb-> right)

{

EA [1] = 1;

If (BC. Y> recta-> bottom) | (BC. Y <recta-> top ))

{

EB [1] = 1;

}

}

Else

{

EB [1] = 1;

If (AC. Y> rectb-> bottom) | (AC. Y <recta-> top ))

{

EA [1] = 1;

}

}

If (recta-> top <rectb-> top)

{

EA [2] = 1;

If (BC. x> recta-> right) | (BC. x <recta-> left ))

{

EB [2] = 1;

}

}

Else

{

EB [2] = 1;

If (AC. x> rectb-> right) | (AC. x <recta-> left ))

{

EA [2] = 1;

}

}

If (recta-> bottom> rectb-> bottom)

{

EA [3] = 1;

If (BC. x> recta-> right) | (BC. x <recta-> left ))

{

EB [3] = 1;

}

}

Else

{

EB [3] = 1;

If (AC. x> rectb-> right) | (AC. x <recta-> left ))

{

EA [3] = 1;

}

}

// ConnectionCodeNot provided. Please complete it on your own.

 

// (4) In the fourth case, a rectangle is contained by another rectangle and does not overlap. The edges of the inner rectangle are connected to the inside of the edges of the outer rectangle.

// (5) in the fifth case, the two rectangles completely overlap and the connections should be invisible.

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.