Code for moving a point in a straight line.

Source: Internet
Author: User

You must add a timer and a button to the form in advance.

 

Private void button#click (Object sender, eventargs e) <br/>{< br/> timer1.enabled = true; <br/>}</P> <p> private point pt1 = new point (10, 10 ); // start coordinate <br/> private point pt2 = new point (300,300); // end coordinate </P> <p> private int n = 0; // distance from the start point of the motion point <br/> private bool bdirect = true; // true indicates that the operation starts from the start point to the end point, false: run from the end point to the start point <br/> point Pt = new point (0, 0); // current point </P> <p> private void timerspontick (Object sender, even Targs e) <br/>{< br/> // Cartesian triangle, used to calculate the length of the Oblique Edge <br/> int M = (INT) math. pow (math. pow (pt2.x-pt1.x), 2) + math. pow (pt2.y-pt1.y), 2), 0.5); // total distance </P> <p> // judge round-trip <br/> If (n> = m) <br/> bdirect = false; <br/> If (n = 0) <br/> bdirect = true; </P> <p> // Based on round-trip, set addition and subtraction <br/> If (bdirect) <br/> N + = 3; <br/> else <br/> N-= 3; </P> <p> pen p1 = new pen (color. yellow); <br/> pen P2 = new pen (color. red); <br/> graphics g = This. creategraphics (); </P> <p> If (Pt. X! = 0 & pt. y! = 0) // clear the last motion point <br/>{< br/> G. drawellipse (new pen (this. backcolor), PT. x-2, PT. y-2, 1, 1); // clear the last motion point <br/>}</P> <p> G. drawline (P1, pt1, pt2); // draw the line from the start point to the end point </P> <p> PT. X = (pt2.x-pt1.x) * N)/m + pt1.x; <br/> PT. y = (pt2.y-pt1.y) * N/m + pt1.y; </P> <p> G. drawellipse (P2, PT. x-2, PT. y-2, 1, 1); // point of draw motion <br/> G. dispose (); <br/>}< br/>

Related Article

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.