Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.5 kB
3
Indexable
Never
def calculate_area(x, y):
    return np.sqrt(x**2 + y**2) + 2/3 * np.sqrt(x**2 + (5/6 - y)**2)

def generate_points(num_points):
    points = np.random.rand(num_points, 2) * 2 - 1  # Generate random points in the range [-1, 1]
    return points

def main():
    num_points = 1000000
    points = generate_points(num_points)

    outside_points = points[np.where(calculate_area(points[:, 0], points[:, 1]) > 1)]
    inside_points = points[np.where(calculate_area(points[:, 0], points[:, 1]) <= 1)]

    areas = []
    for i in range(1, num_points + 1):
        current_outside_points = outside_points[:i]
        current_area = 4 * len(current_outside_points) / i  # Area estimation using the ratio of outside points
        areas.append(current_area)

        if i % 1000 == 0:
            print(f"Area after {i} points: {current_area:.2f}")

    # Print final result
    final_area = areas[-1]
    print(f"The area outside the Twitter egg is {final_area:.2f}")

    # Plot the distribution of outside and inside points
    plt.scatter(outside_points[:, 0], outside_points[:, 1], color='blue', s=1, label='Outside Points')
    plt.scatter(inside_points[:, 0], inside_points[:, 1], color='red', s=1, label='Inside Points')
    
    # Display the area in the graph
    plt.title(f'Distribution of Points\nFinal Area Estimate: {final_area:.2f}')
    plt.legend()
    plt.show()

if __name__ == "__main__":
    main()
Leave a Comment