Silverlight mathematical engine (7) -- Intersection Point 1 of ruler plotting

Source: Internet
Author: User

We have summarized the three major elements (points, lines, circles) of the ruler plot, and come to the conclusion that all figures ultimately depend on some freepoints ). Free points are independent and can be drawn on the screen at will. Therefore, there is no complicated mathematical computation except for the basic Coordinate Transformation (mathematical coordinate system and screen coordinate system), so we will not discuss them. This section mainly discusses the Coordinate Calculation of the intersection of line and line. We would like to put three intersections (line and line, line and Circle, circle and circle) in this section, later, when I wrote the Demo code, I found that the amount of work was a little large, so this section only introduced the intersection of deprecation and line (this is called the small step to run fast, which is a recommended method for TDD programming, because the steps are too big and easy to get rid of, it seems a little egg pain these two days ).

There must be two lines at the intersection of the line and the line. We need to use the plane resolution ry (a combination of algebra and ry) to calculate coordinates in the background. We use the mathematical knowledge of junior high school, therefore, do not panic.

Let's take a look at the linear equation:

Y = ax + B

When we use two free points to determine a straight line, we take two free points A (x1, Y1), B (X2, Y2) the coordinate of A can be used to calculate the values of A and B. For the sake of intuition, we define a line class (logicline ). (The classes defined in this section are basically used to demonstrate the convergence of computing and mathematical concepts. We use the mathematical Coordinate System for reference).

// Indicates a straight line: Y = ax + B public class logicalline {public logicalline (logicalpoint P1, logicalpoint P2) {p1 = p1; P2 = P2;} public logicalpoint P1 {Get; set;} public logicalpoint P2 {Get; set;} // straight line (x1, Y1) (X2, Y2): Y = ax + B: A = (y1-y2) /(x1-x2), B = y1-a * X1 private double dy {get {return p2.y-p1.y;} private double DX {get {return p2.x-p1.x ;}} public double A {get {return dy/dx;} public Double B {get {return p1.y-A * p1.x ;}}}

With the linear equation, the intersection of the two lines is naturally understood as a system of equations (a system of binary equations). Suppose there are two straight lines:

  1. Y = a1x + B1
  2. Y = a2x + b2

If the equations have a solution (x = X3, y = Y3), then the vertex (X3, Y3) is the intersection.

Interestingly, in this equations, if a1 = a2 and b1 = B2, there are no number of solutions. Is there any number of intersections? In this case, the two lines overlap, which can be understood as a special case where two lines are parallel. We think there is no intersection. It's easy to solve the first system of binary equations, right? It's all about algebra. It's so annoying. So when I was in junior high school, I only liked pure ry.

In any case, the one-time equation is quite simple, and the logic is written in the Code. If you have been away from junior high school for many years, you can take a look at it for a short time. Otherwise, you can ignore it completely:

// Public static logicalpoint lineandline (logicalline line1, logicalline line2) {Double X; Double Y; If (line1.dx. iszero () // parallel to Y axis {x = line1.p1. x; y = line2.a * x + line2. B;} else if (line2.dx. iszero () // parallel to Y axis {x = line2.p1. x; y = line1.a * x + line1. B;} else {If (line1.a. isequal (line2.a) // It is considered that the intersection is infinite {x = double. negativeinfinity; y = double. negativeinfinity;} else {var d = (line1.a-line2.a); X = (line2. B-line1. B)/d; y = line1.a * x + line1. B;} return New logicalpoint (X, Y );}

You can see that some extension methods are used in the Code, such as iszero () or something. If you feel confused, you can look at the source code. using too many extension methods may make the Code look a little different. net, after all.. Net class libraries, functions, and other functions are complete. However, they can be used occasionally for ease of writing.

The next step is to test whether our computing logic is correct. Although TDD is essential but not intuitive, we still need to write an interactive interface for verification. The specific method is as follows:

  1. Use the coordinate system we previously implemented (coordinatesystem)
  2. Draw two free vertices p1, p2 (red), and four vertices (green) in the coordinate system ).
  3. Draw four fixed straight lines (the four sides of the canvas where the coordinate system is located ).
  4. Draw a free line freeline (dependent on P1 and P2 ).
  5. Add four intersections (the intersection of freeline and four edges) (blue)
  6. Add the mouse event of the coordinate system so that you can drag P1 and P2.
  7. When P1 or P2 moves, recalculate the coordinates of freeline and the four intersections and update the display.

Let's first design several elements (points and lines) in the coordinate system to implement our test. Although the elements are very few simple, we still follow the abstract to the specific design method, this is a habit. In fact, just draw a few circles, lines, and other things to test. However, it does not save much time, but it is not conducive to cultivating good programming habits.

With the introduction of the object-oriented design method in Section 1, we will not introduce it in detail here. The following figure is provided:

This design reflects the order of dependency. For example, the line depends on two points, and the intersection depends on two lines. The advantage is that as long as a free point changes, in this way, you can find all the relevant elements based on the dependency, and have achieved the goal of taking the lead.

Because the coordinate system used for display and calculation is different, it is necessary to define CS (coordinate system) in the interface, because we use the mathematical Coordinate System for calculation, the display on the interface must follow the screen coordinate system.

Updatevisual () is used to calculate and update positions. Of course, different graphic elements have different logics, which are clear in the code.

Here, by the way, a common function of the canvas mouse event is to take elements from the current click point. For example, there is an ellipse on the canvas. If I click ellipse, I can find this element.

As game programming, people will certainly think of using hittest. Right, we will use it. Here I will extend it into a static method, and it is still generic, in this way, if there are multiple elements under the point, you can easily find out the type you want. For example, the free points and free lines in our test cases overlap partially, we only need to drag points. The Code is as follows, in which visualtreehelper. findelementsinhostcoordinates is the method in the Silverlight Class Library:

public static T HitTest<T>(this Panel panel, Point p) where T : FrameworkElement        {            var t = VisualTreeHelper.FindElementsInHostCoordinates(p, panel).FirstOrDefault();            if (t != null && t is T)                return (T)t;            return null;        }

OK, this section is here. Let's see how to run it:

 

[Source code and demo address]

In the next section, we will explain how to calculate more complex intersections, and how to determine the order of intersections when multiple intersections exist!

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.