C language plane ry 9-intersection of two straight lines

Source: Internet
Author: User

Find the intersection of two straight lines in the plane. When the two straight lines are uneven, there must be only one intersection.

/* Returns the intersection of two straight lines */PointLinesintersection(Line m, line N, int * flag) {double D = n. A * m. b-M. A * n. b; If (D = 0) {* flag = 0; return;} Point I; I. X = (N. B * m. c-M. B * n. c)/d; I. y = (M. A * n. c-n. A * m. c)/d; * flag = 1; return I ;}

The disadvantage of this function is that the parameter flag is added, because the two straight lines M and N may be parallel, and the return value is null.

If you change the function to return a point pointer, the return value is null when the parallel operation is performed.

Why didn't I choose this method? if the pointer is used, you must use malloc in this function to apply for a space. The space must be released outside the function, so it is easy to forget to release it.

About malloc: malloc is the space applied for when the program is running. It is more efficient, and you need to consider the unsuccessful processing of the application and the release of all malloc variables accidentally. My personal principle is to try not to use the malloc variable while solving the problem.

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.