Check if two polygons overlap: Difference between revisions

Content added Content deleted
(Created Nim solution.)
(added Raku programming solution)
Line 517: Line 517:
poly2 and poly3 overlap? true
poly2 and poly3 overlap? true
</pre>
</pre>

=={{header|Raku}}==
{{trans|Go}}
<syntaxhighlight lang="raku" line># 20230810 Raku programming solution

class Vector2 { has ( $.x, $.y );
method dot ( \other ) { self.x * other.x + self.y * other.y }
};

class Projection { has ( $.min, $.max ) };

sub getAxes ( \poly ) {
return poly.append(poly[0]).rotor(2=>-1).map: -> (\vertex1,\vertex2) {
my \vector1 = Vector2.new: x => vertex1[0], y => vertex1[1];
my \vector2 = Vector2.new: x => vertex2[0], y => vertex2[1];
my \edge = Vector2.new: x => vector1.x - vector2.x,
y => vector1.y - vector2.y;
$_ = Vector2.new: x => -edge.y, y => edge.x
}
}

sub projectOntoAxis ( \poly, \axis ) {
my \vertex0 = poly[0];
my \vector0 = Vector2.new: x => vertex0[0], y => vertex0[1];
my $max = my $min = axis.dot: vector0;
for poly -> \vertex {
my \vector = Vector2.new: x => vertex[0], y => vertex[1];
given axis.dot: vector { when $_ < $min { $min = $_ }
when $_ > $max { $max = $_ } }
}
return Projection.new: min => $min, max => $max
}

sub projectionsOverlap ( \proj1, \proj2 ) {
return False if ( proj1.max < proj2.min or proj2.max < proj1.min );
return True
}

sub polygonsOverlap( \poly1, \poly2 ) {
my (\axes1,\axes2) := (poly1,poly2).map: { getAxes $_ };
for (axes1, axes2) -> \axes {
for axes -> \axis {
my (\proj1,\proj2) := (poly1,poly2).map: { projectOntoAxis $_, axis }
return False unless projectionsOverlap(proj1, proj2)
}
}
return True
}

my \poly1 = [ <0 0>, <0 2>, <1 4>, <2 2>, <2 0> ];
my \poly2 = [ <4 0>, <4 2>, <5 4>, <6 2>, <6 0> ];
my \poly3 = [ <1 0>, <1 2>, <5 4>, <9 2>, <9 0> ];

say "poly1 = ", poly1;
say "poly2 = ", poly2;
say "poly3 = ", poly3;
say();
say "poly1 and poly2 overlap? ", polygonsOverlap(poly1, poly2);
say "poly1 and poly3 overlap? ", polygonsOverlap(poly1, poly3);
say "poly2 and poly3 overlap? ", polygonsOverlap(poly2, poly3);</syntaxhighlight>
You may [https://ato.pxeger.com/run?1=jVXNbptAED5W4ilGkQ-4xQjWbtQY21UuvaaHqpc4imhY27QEEOAEZPlJesmhfak-TWd2lh9j1w0HdnZ2vpnZ-Wbg56_M_7F9efm9LVajD3_eBA-Rn-fwVT4USSZgBxs_BxMGdmnhq4KhZwDAoyw2SQBBUuDZMik2MoMhGucyWtklvAWlQukdq6pGVcHe2HtgGBznc5Z8x1BhEndCPYYxBXv0S3S69wwj336DtSyuS0kGyzSJKgpHmWSy2GYxkMr201TGgUnyrXM3tLME72CK-WLkDtFbOoXRAszlk8wKWbqWFoT2RNeqAJV0cxfmdQ3sWD5PoYT5AjQSnVtQdRXunXfkQpxxIfouRM-FDNaS5NMuVIZY3ZGWBbGj0WeeqouuOuiqDj24PxlxROnYlc5YbUpCIJVMTso03sRFcl2GDUkWLH3a6gpzbei6DobRNHntCYV1zlTN6VfNqauG-AG1C5dM7cIYJYpuY5dO9VUdZb1KMhWd2kEndKIFziTSz6ND3jp8kvFRXGzu5w3qsb4zzm1Xp4iq_X-5q8ELvuaOFw3eMxXtMLRDxZmrQAsV0AIFZD899tA-v8ELRX6qCESta_EqeuP2yY9yCeEK7ZSZmtWZkgWNL1CFedMcuOqAvx_azZdsK5skkJB1m4HuIJUArqLTQyY2lcxxfmnB6Z3OwWRbZalHfdd8MahIDe8mY0FjqQNIrPknE7Xng7A5gDo2l4Wr8u_Y_YEY3FuqKZgnfg5quY0jid_DYypMzYMOqOFHnNelpP5VCWF33MLMAWdh0SJocWFCi-CdwDPAxq0hgiEThkzY6D1DLnl32YOMGeIyxD2AXPHuSkOM3K_gok7twlKEu16rFq1adNTjVj1WanPodX35ccAYSLhgH2vzbjfpXmKSTuLHr8WPhwdJvx4vGjz_bPU_t_73_gU Attempt This Online!]


=={{header|Wren}}==
=={{header|Wren}}==