Faces from a mesh: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: piddle) |
|||
Line 342: | Line 342: | ||
((8 14) (17 19) (10 12) (10 14) (12 17) (8 18) (18 19)) ==> (8 14 10 12 17 19 18) |
((8 14) (17 19) (10 12) (10 14) (12 17) (8 18) (18 19)) ==> (8 14 10 12 17 19 18) |
||
((1 3) (9 11) (3 11) (1 11)) ==> (Invalid edge format)</pre> |
((1 3) (9 11) (3 11) (1 11)) ==> (Invalid edge format)</pre> |
||
=={{header|Phix}}== |
|||
<lang Phix>function perequiv(sequence a, b) |
|||
-- Works by aligning and rotating in one step, so theoretically much faster on massive sets. |
|||
bool res = (length(a)==length(b)) |
|||
if res and length(a)>0 then |
|||
integer k = find(a[1],b) |
|||
if k=0 then |
|||
res = false |
|||
else |
|||
-- align with a (ie make b[1]==a[1], by |
|||
-- rotating b k places in one operation) |
|||
b = b[k..$]&b[1..k-1] |
|||
if a!=b then |
|||
-- eg {8,3,4,5} <=> {8,5,4,3}, ie |
|||
-- rotate *and* keep in alignment. |
|||
b[2..$] = reverse(b[2..$]) |
|||
res = (a==b) |
|||
end if |
|||
end if |
|||
end if |
|||
-- return res |
|||
return {"false","true"}[res+1] |
|||
end function |
|||
function edge2peri(sequence edges) |
|||
sequence was = edges, res = {} |
|||
string error = "" |
|||
integer lnk |
|||
if length(edges)<2 then |
|||
error = "too short" |
|||
else |
|||
-- edges = sort(edges) -- (see note below) |
|||
res = edges[1] |
|||
edges = edges[2..$] |
|||
lnk = res[2] |
|||
while length(edges) and error="" do |
|||
bool found = false |
|||
for i=1 to length(edges) do |
|||
integer k = find(lnk,edges[i]) |
|||
if k then |
|||
lnk = edges[i][3-k] |
|||
edges[i..i] = {} |
|||
if edges={} then |
|||
if lnk!=res[1] then error = "oh dear" end if |
|||
else |
|||
if find(lnk,res) then error = "oops" end if |
|||
res &= lnk |
|||
end if |
|||
found = true |
|||
exit |
|||
end if |
|||
end for |
|||
if not found then error = "bad link" exit end if |
|||
end while |
|||
end if |
|||
if length(error) then res = {error,res,lnk,edges,was} end if |
|||
return res |
|||
end function |
|||
constant ptests = {{{8, 1, 3}, {1, 3, 8}}, |
|||
{{18, 8, 14, 10, 12, 17, 19}, {8, 14, 10, 12, 17, 19, 18}}, |
|||
-- (check our results below against Go etc) |
|||
{{1,11,7},{1,7,11}}, |
|||
{{11,23,17,1},{1,11,23,17}}} |
|||
for i=1 to length(ptests) do |
|||
sequence {p,q} = ptests[i] |
|||
printf(1,"%v equivalent to %v: %s\n",{p,q,perequiv(p,q)}) |
|||
end for |
|||
constant etests = {{{1, 11}, {7, 11}, {1, 7}}, |
|||
{{11, 23}, {1, 17}, {17, 23}, {1, 11}}, |
|||
{{8, 14}, {17, 19}, {10, 12}, {10, 14}, {12, 17}, {8, 18}, {18, 19}}, |
|||
{{1, 3}, {9, 11}, {3, 11}, {1, 11}}} |
|||
for i=1 to length(etests) do |
|||
printf(1,"%v\n",{edge2peri(etests[i])}) |
|||
end for</lang> |
|||
{{out}} |
|||
(second part matches Julia/Perl: un-comment that sort above to match Go/Python/zkl) |
|||
<pre> |
|||
{8,1,3} equivalent to {1,3,8}: true |
|||
{18,8,14,10,12,17,19} equivalent to {8,14,10,12,17,19,18}: true |
|||
{1,11,7} equivalent to {1,7,11}: true |
|||
{11,23,17,1} equivalent to {1,11,23,17}: true |
|||
{1,11,7} |
|||
{11,23,17,1} |
|||
{8,14,10,12,17,19,18} |
|||
{"bad link",{1,3,11,9},9,{{1,11}},{{1,3},{9,11},{3,11},{1,11}}} |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |