Come and play and draw a straight line

Source: Internet
Author: User

Oh, la, I 've been here again. I don't know if I really want to hear about it for a few months. I have dug haha ;).

YanbenArticleI hope you will not lose the bricks.

WrittenProgramThat is to say, it's a pleasure to be a cool guy in your blog parks.

This time, we came to show off the straight line equation with a slight smile. The most widely used linear equation was. net. So we still did the test on the. NET platform.

Theme:

First, move the callback function out.

Linear equation: Y = ax + B

Everyone on Earth knows this. If you are a junior high school student, skip this step...

And what is the oblique side of an equi-edge triangle with a right angle of 1 on both sides? Of course it's root 2. Hahaha

I usually use lineto () in VC before lineto (). Didn't my shoes ever think about how he came from.

If you know the two coordinates (x1, Y1) (X2, Y2 ), that is to say, if your mouse clicks twice in different places on your form1, then two point objects will understand the difference.

There are many "styles" for linear equations, such as the point-oblique method, the haidilao method, the old-tree method, the two-point method, and the three-point method,

Because we know two vertices (coordinates) in advance, we certainly use the two-point formula:

(Y-y1)/(y2-y1) = (x-x1)/(x2-x1)

Y2 Y1 X2 I know everything about X1. Are you familiar with Y? That's it...

The formula above was copied from Baidu, where he was wrong.

The final findings above won't work, so they are basically nonsense.

The solutions we use now:

Now that we know the two coordinates (the values of X and Y), we will establish a system of binary equations following y = ax + B.

If this doesn't make sense, let's talk about it first, for example, P1 = (0, 1) P2 = (5, 4)

Substitution Equation

1 = A * 0 + B

4 = A * 5 + B

Don't tell me that you won't solve a binary system.

First B = y1-a * X1 into 2 type

A = (y2-y1)/(x2-x1)

Then

B = y1-a * X1

This is a good solution. Anyway, the coordinates X1 must not be equal to X2; otherwise, it is a vertical line,

Let X1 accumulate or accumulate in the direction of X2 and then continuously find the value of Y. Isn't it a straight line?

Digging, haha, finally OK

Don't be too happy,

The coordinate system in Windows is like this:

The Cartesian coordinate system in mathematics is like this:

To do this, we only need to perform this operation (we can see the broken figure above, and I have compiled it into the article on tetris ):

 
// Step 1 // convert windows coordinate x y to negative because of the differences between Windows coordinate and mathematical coordinate system (assuming that x y is the third quadrant of mathematical coordinates) point T1 = new point (0-p1.x, 0-p1.y); point t2 = new point (0-p2.x, 0-p2.y );

I don't explain why. Some friends may say that it's okay to change X to a negative number.

Why. I have not drawn the first to third photo limitations of the coordinate system, which may be confusing to everyone.

And listen to the poor, slowly say:

In the Windows coordinate system, x y is a positive number, which is projected to the third quadrant of the Cartesian coordinate system (that is, x y is a negative number ).

However, the line after the projection in the third quadrant is symmetric with the line in the Fourth quadrant. Didn't you find out?

In short, the above meaning can be expressed as follows:

How does one change back to the fourth quadrant in windows? Let's just say, it's okay to take the absolute value of all the final results:

 
// Note the transformation from a plane Cartesian coordinate system to a Windows coordinate system drawpoint (new point (INT) math. Abs (I), (INT) math. Abs (TY )));

At last, I complained that bitmap has the setpixel () function graphics, but I am not looking for a picture function. Maybe I have never seen it before.

You only need to complete the following items:

 
Void drawpoint (point P1) // The painting Point P1 is the Windows coordinate {bitmap P = new Bitmap (); graphics G = graphics. fromimage (p); G. clear (color. red); gph. drawimage (p, new point (p1.x, p1.y ));}

To be honestCodeThere are dozens of lines without Doha, and the key is theory. It's a scorpion, but it's a horse:

Void drawpoint (point P1) // The painting Point P1 is the Windows coordinate {bitmap P = new Bitmap (); graphics G = graphics. fromimage (p); G. clear (color. red); gph. drawimage (p, new point (p1.x, p1.y);} void drawline (point P1, point P2) // The line-drawn function P1 P2 is Windows coordinate {// linear equation // y = AX + B // Step 1 // because of the differences between Windows coordinate and mathematical coordinate, the Windows Coordinate x y turns to negative (assuming it is the third quadrant in mathematical coordinates) point T1 = new point (0-p1.x, 0-p1.y); point t2 = new point (0-p2.x, 0-p2.y ); // Step 2 // follow the linear equation to create a dual system of dual units and solve the double x1, x2, Y1, Y2; X1 = t1.x; x2 = t2.x; y1 = t1.y; y2 = t2.y; Double A, B; // connection Cube Run // Y1 = ax1 + B // y2 = ax2 + B // replace B = y1-ax1 with Type 2 // y2 = ax2 + (y1-ax1) // y2-y1 = ax2-ax1 // y2-y1 = a (x2-x1) // so // A = (y2-y1)/(x2-x1) A = (Y2-Y1) /(x2-X1); // then B = Y1-A * x1; // step 3 // substitute X and draw a straight line for (double I = x1; i> = x2; I-= 1) {double Tx, Ty; ty = A * I + B; // note the transformation from a plane Cartesian coordinate system to a Windows coordinate system drawpoint (new point (INT) math. ABS (I), (INT) math. ABS (TY )));}}

Call:

 
// Two points x = 20 Y = 20, x = 50 y = 50 drawline (new point (20, 20), new point (50, 50 ));

Call result

Dangdang:

There are still many bugs and deficiencies in the program. Forgive me. Don't lose the tomato, the rotten eggs, and so on. Haha □ ̄ |

Because the technology content is too low, the big wet people in the blog Park have always been very disdainful to me. This time, it's a little technical ;).

Source code download

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.