Python Method for drawing triangle outer and incircle, python outer circle

Source: Internet
Author: User

Python Method for drawing triangle outer and incircle, python outer circle

I just watched the Sino-English showdown in "strongest brain". The most difficult project requires contestants to complement the tyerson polygon first, and then find two identical Tyson polygon. While stunned and sighing that my mind is stupid, I can't help but try to figure it out with my computer. It's also great to be idle and boast nothing.

Today, we will first draw an external circle and an incircle, and leave it behind a big pitfall.

Circle Center: the point of intersection of the triangle vertical bisector.
Circle Center: the point of intersection of triangle angular bisector.

With the idea, you can use the omnipotent python for computing.

Import matplotlib. pyplot as pltfrom scipy. linalg import solveimport numpy as npfrom matplotlib. patches import Circle ''' calculate the outer and incircle of A triangle ''' # Draw A triangle def plot_triangle (A, B, C): x = [A [0], B [0], C [0], A [0] y = [A [1], B [1], C [1], A [1] ax = plt. gca () ax. plot (x, y, linewidth = 2) # Draw a circle def draw_circle (x, y, r): ax = plt. gca () cir = Circle (xy = (x, y), radius = r, alpha = 0.5) ax. add_patch (cir) ax. plot () # Round def get_out Er_circle (A, B, C): xa, ya = A [0], A [1] xb, yb = B [0], B [1] xc, yc = C [0], C [1] # midpoint x1, y1 = (xa + xb)/2.0, (ya + yb)/2.0x2, y2 = (xb + xc)/2.0, (yb + yc)/2.0 # slope of the two lines ka = (yb-ya)/(xb-xa) if xb! = Xa else None kb = (yc-yb)/(xc-xb) if xc! = Xb else None alpha = np. arctan (ka) if ka! = None else np. pi/2 beta = np. arctan (kb) if kb! = None else np. pi/2 # slope of the two vertical bisector k1 = np. tan (alpha + np. pi/2) k2 = np. tan (beta + np. pi/2) # center y, x = solve ([[1.0,-k1], [1.0,-k2], [y1-k1 * x1, y2-k2 * x2]) # radius r1 = np. sqrt (x-xa) ** 2 + (y-ya) ** 2) return (x, y, r1) # Round def get_inner_circle (A, B, C ): xa, ya = A [0], A [1] xb, yb = B [0], B [1] xc, yc = C [0], C [1] ka = (yb-ya)/(xb-xa) if xb! = Xa else None kb = (yc-yb)/(xc-xb) if xc! = Xb else None alpha = np. arctan (ka) if ka! = None else np. pi/2 beta = np. arctan (kb) if kb! = None else np. pi/2 a = np. sqrt (xb-xc) ** 2 + (yb-yc) ** 2) B = np. sqrt (xa-xc) ** 2 + (ya-yc) ** 2) c = np. sqrt (xa-xb) ** 2 + (ya-yb) ** 2) ang_a = np. arccos (B ** 2 + c ** 2-a ** 2)/(2 * B * c) ang_ B = np. arccos (a ** 2 + c ** 2-B ** 2)/(2 * a * c) # slope of the two angular bisector k1 = np. tan (alpha + ang_a/2) k2 = np. tan (beta + ang_ B/2) kv = np. tan (alpha + np. pi/2) # Calculate the center y, x = solve ([[1.0,-k1], [1.0,-k2], [ya-k1 * xa, yb-k2 * xb]) ym, xm = solve ([1.0,-ka], [1.0,-kv], [ya-ka * xa, y-kv * x]) r1 = np. sqrt (x-xm) ** 2 + (y-ym) ** 2) return (x, y, r1) if _ name _ = '_ main _': A = (1 ., 1 .) B = (5 ., 2 .) C = (5 ., 5 .) plt. axis ('equal') plt. axis ('off') plot_triangle (A, B, C) x, y, r1 = get_outer_circle (A, B, C) plt. plot (x, y, 'ro') draw_circle (x, y, r1) x_inner, y_inner, r_inner = get_inner_circle (A, B, C) plt. plot (x_inner, y_inner, 'ro') draw_circle (x_inner, y_inner, r_inner) plt. show ()

Let's take a look at the results of the two triangles:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.