[Original] C # draw three contour lines

Source: Internet
Author: User

Reprinted, please indicate the author and the source. Thank you.

We have mentioned the contour tracing solution above. On this basis, we can draw the contour lines, but it is still not possible to draw only bare lines without annotation, where do others know what a line like a worm represents? In this article, we will discuss how to label the contour.

I would like to thank the author of the article "exploring an algorithm for contour labeling". I am using this paper as an important part.AlgorithmGuided my work.

First, mark the small closed contour lines.

I didn't come up with a good method here. I used the method to find out the X minimum, Y minimum, x maximum, and maximum coordinates of the enclosed contour point; if xmax-xmin <specified value and Ymax-ymin <specified value, then P (xmin + xmax)/2, (ymin + Ymax)/2) draw the value of the contour line, as shown below:CodeAs shown in:

If (points [0]. X = points [points. count-1]. X & points [0]. y = points [points. count-1]. y) {float xmin = points. min <vpoint> (P => P. x), xmax = points. max <vpoint> (P => P. x), ymin = points. min <vpoint> (P => P. y), Ymax = points. max <vpoint> (P => P. y); If (xmax-xmin <25 & Ymax-ymin <25) {G. drawstring (value. tostring (), Font, brush, (xmax + xmin-SF. width)/2f, (Ymax + ymin-SF. height)/2f); continue ;}}

Red circles in the effect are shown in the following figure:

Figure 1

After labeling a small closed contour, the labeling method for large points and open contour is the same.

According to the guiding ideology in the article "exploring an algorithm for contour labeling", we need to convert the curve into a line or a polygon. What does it mean? See:

If I tell you, now if the length of each line segment is greater than the elevation VALUE OF THE STRING (value. the length required for tostring ()-the white space length. It is not a problem to draw a label there. For those small line segments, do not draw them, as small as the closed contour, we have processed it in the previous step.

So how can we convert a curve into a line or a polygon? The article "how to explore an algorithm for contour labeling" tells us (see article 2.2): "set a dif parameter to control the deviation of the approximate contour of a polygon, start from 0th points on the curve, and connect the even points to the side of the polygon. view the three points on the contour in sequence) the distance from the edges to the polygon is smaller than the parameter DIF, and the equivalent point in the middle is discarded. Otherwise, the intermediate point is saved. This continues until the number of edges in the last generated polygon is not reduced (think8848 note. In this way, the polygon with the contour similarity is obtained ." The larger the DIF, the steep the curve and the smaller the DIF, the smoother the curve.

Sample algorithm code:

Int n = (points [0]. X! = Points [points. Count-1]. X & points [0]. y! = Points [points. Count-1]. Y )? Points. count: points. count-1, minedge = n + 1, K = N; float tolerance = 8f; var indexes = new list <int> (); For (INT I = 0; I <points. count; I ++) {indexes. add (I) ;}while (k <minedge) {minedge = K; var p = 0; while (P <minedge-(N % 2 = 0? 3: 2) {float straight = This. getpointtostraight (points [indexes [p + 1], points [indexes [p], points [indexes [p + 2]); If (math. ABS (straight) <tolerance) {indexes [p + 1] =-1; k-= 1;} p ++; P ++;} indexes = indexes. where <int> (Index => index! =-1). tolist <int> ();}

The method for calculating the distance from a point to a straight line is attached. The following example shows how to calculate the shortest distance from a point to a straight line P1P2.

 
Private float getpointtostraight (vpoint P, vpoint P1, vpoint P2) {If (p1.x = p2.x) {return (float) math. ABS (p1.y-p2.y);} If (p1.y = p2.y) {return (float) math. ABS (p1.x-p2.x);} Double K = (p2.y-p1.y)/(p2.x-p1.x); double C = (p2.x * p1.y-p1.x * p2.y) /(p2.x-p1.x); Return (float) (K * P. x-P. Y + C)/(math. SQRT (K * k + 1 )));}

Finally, indexes stores the index of points that constitute a polygon in the isovertices list.

At the end of this article, let's talk about the question of character rotation. If there is a line segment as the benchmark, it is not difficult to rotate the canvas and the line segment at the same angle ,. net is easy to do. The only problem is how to tilt the line segment:

 
VaR alpha = (float) (math. atan (p2.y-p1.y)/(p2.x-p1.x) * 180/Math. Pi );

From this point of view, a brother who has forgotten the trigonometric function can go to the Internet to relearn high school mathematics.

G. translatetransform (xoffset, yoffset); G. rotatetransform (alpha); G. drawstring (value. tostring (), Font, brush, new pointf (0, 0); G. rotatetransform (-alpha); G. translatetransform (-xoffset,-yoffset );

Change the origin of the coordinate system to the position in the upper left corner of the annotation to be drawn, and then rotate the canvas. (Note that the angle is clockwise, and the angle is clockwise when the angle is negative, it seems that it is different from the direction in our mathematics class .) After the painting is complete, return the coordinate system to the position and cycle until all the annotations are drawn.

So far, the use of C # To draw the contour is basically complete.

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.