Рубрики
Без рубрики

Площадь пересекающихся кругов

Рассчитать площадь двух пересекающихся кругов на расстоянии между центрами, радиусами двух кругов. Теги с тригонометрией, Highschoolmath, Python, Circle.

Несколько дней назад мои дочери хотели узнать, как выяснить ограниченный участок двух пересекающихся кругов. Эта программа – это попытка научить их как тригонометрии, так и на компьютерную науку. Мы начали делать только базовое условие, когда оба круга играют приятно и пересекаются на расстоянии между центрами, параллельными горизонтальной осью. Медленно мы расширили код, чтобы удовлетворить другие случаи, когда круги только касаются только на одной точке, не могут прикоснуться на всех или один круг полностью внутри другого круга.

По сути, вход принимает три параметра -> расстояние между центрами двух кругов и радиусов двух кругов. Общая область является выходом программы.

Ищете способы сделать эту более оптимальную программу, так как я новичок в Python.

import sys
import math

# Return the area of the smaller circle
def smallercirclearea(r1, r2):
    radsmaller = min(r1, r2)
    return math.pi * radsmaller * radsmaller

def calcAreaIntersectingCircles(d, rad1, rad2):
    rad1sqr = rad1 * rad1
    rad2sqr = rad2 * rad2

    #if the circle centers are the same
    if d == 0:
        print ('\nCircles have the same center. Intersecting area will be area of smaller circle')
        return smallercirclearea(rad1,rad2)

    angle1 = (rad1sqr + (d * d) - rad2sqr) / (2 * rad1 * d)
    angle2 = (rad2sqr + (d * d) - rad1sqr) / (2 * rad2 * d)

    # Check to see if the circles are overlapping
    if ((angle1 < 1 and angle1 >= -1) or (angle2 < 1 and angle2 >= -1)):
        theta1 = (math.acos(angle1) * 2)
        theta2 = (math.acos(angle2) * 2)

        area1 = (0.5 * theta2 * rad2sqr) - (0.5 * rad2sqr * math.sin(theta2))
        area2 = (0.5 * theta1 * rad1sqr) - (0.5 * rad1sqr * math.sin(theta1))

        return area1 + area2
    elif angle1 == 1 and angle2 == 1:
        print ('\nCircles touch at a single degenerate point and do not intersect\n')
        return 0
    elif angle1 < -1 or angle2 < -1:
        print('\nSmaller circle is completely inside the larger circle. Intersecting area will be area of smaller circle')
        return smallercirclearea(rad1,rad2)
    else:
        print ('\nImaginary touch points\n')
        return -1

#Read the distance and radii from the command line
if len(sys.argv) == 4:
    distance = int(sys.argv[1])
    radiusCircle1 = int(sys.argv[2])
    radiusCircle2 = int(sys.argv[3])
    if distance < 0 or radiusCircle1 < 0 or radiusCircle2 < 0:
        print('Values cannot be negative. Goodbye')
        exit(1)

    print('\nThe intersecting area of the two circles is', round(calcAreaIntersectingCircles(float(distance), float(radiusCircle1), float(radiusCircle2)), 2), 'square units\n')
else:
    print('\nNEED 3 VALUES IN THE INPUT!!\n')
    exit(1)

Оригинал: “https://dev.to/bacchu/area-of-intersecting-circles–36l0”