35 std::cout <<
"Point 1: " << p1 << std::endl;
36 std::cout <<
"Point 2: " << p2 << std::endl;
37 std::cout <<
"Distance from Point 1 to Point 2: " << std::fixed << std::setprecision(3)
44 std::cout <<
"\nRectangle 1: " << rect1 << std::endl;
45 std::cout <<
"Rectangle 2: " << rect2 << std::endl;
46 std::cout <<
"Rectangles intersect: " << (rect1.
intersects(rect2) ?
"Yes" :
"No") << std::endl;
47 std::cout <<
"Point " << p3 <<
" is inside Rectangle 1: " << (rect1.
contains_point(p3) ?
"Yes" :
"No") << std::endl;
52 std::cout <<
"\nTriangle: " << triangle << std::endl;
53 std::cout <<
"Triangle area: " << std::fixed << std::setprecision(2)
54 << triangle.
area() << std::endl;
55 std::cout <<
"Triangle is convex: " << (triangle.is_convex() ?
"Yes" :
"No") << std::endl;
62 std::vector<Polygon> test_polygons = {
77 std::vector<std::string> polygon_names = {
78 "Standard Triangle",
"Sharp Angle Polygon",
"L-Shape Polygon",
"Acute Triangle"
81 double threshold_degrees = 45.0;
82 std::cout <<
"Sharp angle threshold: " << threshold_degrees <<
" degrees" << std::endl;
84 for (
size_t i = 0; i < test_polygons.size(); ++i) {
85 std::cout <<
"\n--- " << polygon_names[i] <<
" ---" << std::endl;
87 auto sharp_angles = test_polygons[i].get_sharp_angles(threshold_degrees);
88 std::cout <<
"Found " << sharp_angles.size() <<
" sharp angles" << std::endl;
90 if (!sharp_angles.empty()) {
91 std::cout <<
"Sharp angle positions and angles:" << std::endl;
92 for (
size_t vertex_idx : sharp_angles) {
93 double angle = test_polygons[i].vertex_angle(vertex_idx);
94 std::cout <<
" Vertex " << vertex_idx <<
": "
95 << std::fixed << std::setprecision(1) << angle <<
"°" << std::endl;
100 auto all_angles = test_polygons[i].all_vertex_angles();
101 std::cout <<
"All vertex angles: ";
102 for (
size_t j = 0; j < all_angles.size(); ++j) {
103 if (j > 0) std::cout <<
", ";
104 std::cout << std::fixed << std::setprecision(1) << all_angles[j] <<
"°";
106 std::cout << std::endl;
114 std::vector<std::pair<Polygon, Polygon>> test_pairs = {
128 std::vector<std::string> pair_names = {
129 "Adjacent Rectangles",
"Overlapping Rectangles",
"Adjacent Triangles"
132 double threshold_distance = 2.0;
133 std::cout <<
"Narrow distance threshold: " << threshold_distance <<
" units" << std::endl;
135 for (
size_t i = 0; i < test_pairs.size(); ++i) {
136 std::cout <<
"\n--- " << pair_names[i] <<
" ---" << std::endl;
138 const auto& poly1 = test_pairs[i].first;
139 const auto& poly2 = test_pairs[i].second;
142 double min_distance = poly1.distance_to(poly2);
143 std::cout <<
"Minimum distance: " << std::fixed << std::setprecision(3)
144 << min_distance <<
" units" << std::endl;
147 auto narrow_regions = poly1.find_narrow_regions(poly2, threshold_distance);
148 std::cout <<
"Narrow distance regions: " << narrow_regions.size() << std::endl;
150 if (!narrow_regions.empty()) {
151 std::cout <<
"Narrow distance details:" << std::endl;
152 for (
size_t j = 0; j < narrow_regions.size(); ++j) {
153 auto [point1, point2,
distance] = narrow_regions[j];
154 std::cout <<
" Region " << (j+1) <<
": " << point1 <<
" to " << point2
155 <<
", distance " << std::fixed << std::setprecision(3) <<
distance << std::endl;
160 if (poly1.intersects(poly2)) {
161 std::cout <<
"WARNING: Polygons intersect!" << std::endl;
162 auto intersections = poly1.intersection_points(poly2);
163 std::cout <<
"Intersection points: " << intersections.size() << std::endl;
169 print_separator(
"Interview Problem 3: Quadtree-Optimized Edge Intersection");
172 std::vector<Rectangle> rectangles = {
192 std::cout <<
"Inserting " << rectangles.size() <<
" rectangle components into quadtree..." << std::endl;
195 auto start_time = std::chrono::high_resolution_clock::now();
196 for (
size_t i = 0; i < rectangles.size(); ++i) {
197 bool success = rect_quadtree.insert(rectangles[i]);
198 std::cout <<
" Component " << (i+1) <<
": " << (success ?
"Success" :
"Failed") << std::endl;
200 auto end_time = std::chrono::high_resolution_clock::now();
201 auto insert_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
203 std::cout <<
"Insertion time: " << insert_duration.count() <<
" microseconds" << std::endl;
204 std::cout <<
"Quadtree size: " << rect_quadtree.size() <<
" objects" << std::endl;
207 std::cout <<
"\n--- Range Query Test ---" << std::endl;
210 start_time = std::chrono::high_resolution_clock::now();
211 auto objects_in_region = rect_quadtree.query_range(query_region);
212 end_time = std::chrono::high_resolution_clock::now();
213 auto query_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
215 std::cout <<
"Query region " << query_region << std::endl;
216 std::cout <<
"Found " << objects_in_region.size() <<
" objects" << std::endl;
217 std::cout <<
"Query time: " << query_duration.count() <<
" microseconds" << std::endl;
220 std::cout <<
"\n--- Point Query Test ---" << std::endl;
221 Point query_point(12, 12);
223 start_time = std::chrono::high_resolution_clock::now();
224 auto containing_objects = rect_quadtree.query_point(query_point);
225 end_time = std::chrono::high_resolution_clock::now();
226 auto point_query_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
228 std::cout <<
"Query point " << query_point << std::endl;
229 std::cout <<
"Found " << containing_objects.size() <<
" objects containing the point" << std::endl;
230 std::cout <<
"Point query time: " << point_query_duration.count() <<
" microseconds" << std::endl;
233 std::cout <<
"\n--- Potential Intersection Detection ---" << std::endl;
234 start_time = std::chrono::high_resolution_clock::now();
235 auto potential_intersections = rect_quadtree.find_potential_intersections();
236 end_time = std::chrono::high_resolution_clock::now();
237 auto intersection_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
239 std::cout <<
"Found " << potential_intersections.size() <<
" pairs of potentially intersecting objects" << std::endl;
240 std::cout <<
"Intersection detection time: " << intersection_duration.count() <<
" microseconds" << std::endl;
243 std::cout <<
"\nVerifying actual intersections:" << std::endl;
244 size_t actual_intersections = 0;
245 for (
const auto& pair : potential_intersections) {
246 if (pair.first.intersects(pair.second)) {
247 actual_intersections++;
248 std::cout <<
" Actual intersection: " << pair.first <<
" with " << pair.second << std::endl;
251 std::cout <<
"Actual intersection pairs: " << actual_intersections << std::endl;
254 auto stats = rect_quadtree.get_statistics();
255 std::cout <<
"\n--- Quadtree Performance Statistics ---" << std::endl;
256 std::cout <<
"Total nodes: " << stats.total_nodes << std::endl;
257 std::cout <<
"Leaf nodes: " << stats.leaf_nodes << std::endl;
258 std::cout <<
"Max depth: " << stats.max_depth_reached << std::endl;
259 std::cout <<
"Average objects per leaf: " << std::fixed << std::setprecision(2)
260 << stats.average_objects_per_leaf << std::endl;
261 std::cout <<
"Tree efficiency: " << std::fixed << std::setprecision(3)
262 << stats.tree_efficiency << std::endl;
269 std::vector<Polygon> eda_components = {
290 std::vector<std::string> component_names = {
291 "Microcontroller",
"Resistor",
"Capacitor",
"Connector",
"Trace",
"Component A",
"Component B"
295 struct ProcessRules {
298 double sharp_angle_limit;
301 std::vector<ProcessRules> processes = {
302 {
"Prototype", 0.1, 20.0},
303 {
"Standard", 0.15, 30.0},
304 {
"High Precision", 0.05, 45.0}
307 std::cout <<
"Analyzing " << eda_components.size() <<
" EDA components..." << std::endl;
309 for (
const auto& process : processes) {
310 std::cout <<
"\n--- " << process.name <<
" Process Check ---" << std::endl;
311 std::cout <<
"Min spacing: " << process.min_spacing <<
", Sharp angle limit: "
312 << process.sharp_angle_limit <<
"°" << std::endl;
317 std::cout <<
"\nSharp angle check:" << std::endl;
318 for (
size_t i = 0; i < eda_components.size(); ++i) {
319 auto sharp_angles = eda_components[i].get_sharp_angles(process.sharp_angle_limit);
320 if (!sharp_angles.empty()) {
321 violations +=
static_cast<int>(sharp_angles.size());
322 std::cout <<
" " << component_names[i] <<
": " << sharp_angles.size()
323 <<
" sharp angle violations" << std::endl;
328 std::cout <<
"\nSpacing check:" << std::endl;
329 for (
size_t i = 0; i < eda_components.size(); ++i) {
330 for (
size_t j = i + 1; j < eda_components.size(); ++j) {
331 double distance = eda_components[i].distance_to(eda_components[j]);
332 if (
distance < process.min_spacing) {
334 std::cout <<
" " << component_names[i] <<
" and " << component_names[j]
335 <<
": distance " << std::fixed << std::setprecision(3)
336 <<
distance <<
" < " << process.min_spacing << std::endl;
342 std::cout <<
"\nIntersection check:" << std::endl;
343 for (
size_t i = 0; i < eda_components.size(); ++i) {
344 for (
size_t j = i + 1; j < eda_components.size(); ++j) {
345 if (eda_components[i].intersects(eda_components[j])) {
347 std::cout <<
" " << component_names[i] <<
" and " << component_names[j]
348 <<
": intersection violation" << std::endl;
354 if (violations == 0) {
355 std::cout <<
"PASS: " << process.name <<
" process check" << std::endl;
357 std::cout <<
"FAIL: " << violations <<
" violations, does not meet " << process.name
358 <<
" process requirements" << std::endl;