Untitled
unknown
plain_text
2 years ago
1.5 kB
5
Indexable
import matplotlib.pyplot as plt import numpy as np 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) correct_points = points[np.where(calculate_area(points[:, 0], points[:, 1]) <= 1)] incorrect_points = points[np.where(calculate_area(points[:, 0], points[:, 1]) > 1)] areas = [] for i in range(1, num_points + 1): current_correct_points = correct_points[:i] current_area = 4 * len(current_correct_points) / i # Area estimation using the ratio of correct points areas.append(current_area) if i % 1000 == 0: print("Area after {i} points: {current_area:.2f}") # Print final result final_area = areas[-1] print("The area of the Twitter egg is {final_area:.2f}") # Plot the distribution of correct and incorrect points plt.scatter(correct_points[:, 0], correct_points[:, 1], color='blue', s=1, label='Correct Points') plt.scatter(incorrect_points[:, 0], incorrect_points[:, 1], color='red', s=1, label='Incorrect Points') # Display the area in the graph plt.title('Distribution of Points\nFinal Area Estimate: {final_area:.2f}') plt.legend() plt.show() if __name__ == "__main__": main()
Editor is loading...
Leave a Comment