Sutherland-Hodgman polygon clipping: Difference between revisions
Content added Content deleted
m (→{{header|OCaml}}: update api doc for Graphics module) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 24: | Line 24: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F clip(subjectPolygon, clipPolygon) |
||
F inside(p, cp1, cp2) |
F inside(p, cp1, cp2) |
||
R (cp2.x - cp1.x) * (p.y - cp1.y) > (cp2.y - cp1.y) * (p.x - cp1.x) |
R (cp2.x - cp1.x) * (p.y - cp1.y) > (cp2.y - cp1.y) * (p.x - cp1.x) |
||
Line 59: | Line 59: | ||
V subjectp = [(50.0, 150.0), (200.0, 50.0), (350.0, 150.0), (350.0, 300.0), (250.0, 300.0), (200.0, 250.0), (150.0, 350.0), (100.0, 250.0), (100.0, 200.0)] |
V subjectp = [(50.0, 150.0), (200.0, 50.0), (350.0, 150.0), (350.0, 300.0), (250.0, 300.0), (200.0, 250.0), (150.0, 350.0), (100.0, 250.0), (100.0, 200.0)] |
||
V clipp = [(100.0, 100.0), (300.0, 100.0), (300.0, 300.0), (100.0, 300.0)] |
V clipp = [(100.0, 100.0), (300.0, 100.0), (300.0, 300.0), (100.0, 300.0)] |
||
print_elements(clip(subjectp, clipp), sep' "\n")</ |
print_elements(clip(subjectp, clipp), sep' "\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 76: | Line 76: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Containers.Doubly_Linked_Lists; |
||
with Ada.Text_IO; |
with Ada.Text_IO; |
||
Line 188: | Line 188: | ||
Result := Clip (Source, Clipper); |
Result := Clip (Source, Clipper); |
||
Print (Result); |
Print (Result); |
||
end Main;</ |
end Main;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{ |
<pre>{ |
||
Line 205: | Line 205: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> VDU 23,22,200;200;8,16,16,128 |
||
VDU 23,23,2;0;0;0; |
VDU 23,23,2;0;0;0; |
||
Line 281: | Line 281: | ||
NEXT |
NEXT |
||
DRAW poly{(0)}.x, poly{(0)}.y |
DRAW poly{(0)}.x, poly{(0)}.y |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
[[Image:suthhodg_bbc.gif]] |
[[Image:suthhodg_bbc.gif]] |
||
=={{header|C}}== |
=={{header|C}}== |
||
Most of the code is actually storage util routines, such is C. Prints out nodes, and writes test.eps file in current dir. |
Most of the code is actually storage util routines, such is C. Prints out nodes, and writes test.eps file in current dir. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 464: | Line 464: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight>{{out}}<pre>200 250 |
||
175 300 |
175 300 |
||
125 300 |
125 300 |
||
Line 483: | Line 483: | ||
Worker class: |
Worker class: |
||
< |
<syntaxhighlight lang="c sharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 719: | Line 719: | ||
#endregion |
#endregion |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Window code: |
Window code: |
||
< |
<syntaxhighlight lang="html"> |
||
<Window x:Class="Sutherland.MainWindow" |
<Window x:Class="Sutherland.MainWindow" |
||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
||
Line 744: | Line 744: | ||
</Grid> |
</Grid> |
||
</Window> |
</Window> |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="c sharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 881: | Line 881: | ||
#endregion |
#endregion |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
[[File:PolyIntersect.png]] |
[[File:PolyIntersect.png]] |
||
Line 887: | Line 887: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
using namespace std; |
using namespace std; |
||
Line 1,000: | Line 1,000: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Clipped polygon points: |
<pre>Clipped polygon points: |
||
Line 1,015: | Line 1,015: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.array, std.range, std.typecons, std.algorithm; |
||
struct Vec2 { // To be replaced with Phobos code. |
struct Vec2 { // To be replaced with Phobos code. |
||
Line 1,134: | Line 1,134: | ||
saveEPSImage("sutherland_hodgman_clipping_out.eps", |
saveEPSImage("sutherland_hodgman_clipping_out.eps", |
||
subjectPolygon, clippingPolygon, clipped); |
subjectPolygon, clippingPolygon, clipped); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>immutable(Vec2)(100, 116.667) |
<pre>immutable(Vec2)(100, 116.667) |
||
Line 1,151: | Line 1,151: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule SutherlandHodgman do |
||
defp inside(cp1, cp2, p), do: (cp2.x-cp1.x)*(p.y-cp1.y) > (cp2.y-cp1.y)*(p.x-cp1.x) |
defp inside(cp1, cp2, p), do: (cp2.x-cp1.x)*(p.y-cp1.y) > (cp2.y-cp1.y)*(p.x-cp1.x) |
||
Line 1,188: | Line 1,188: | ||
SutherlandHodgman.polygon_clipping(subjectPolygon, clipPolygon) |
SutherlandHodgman.polygon_clipping(subjectPolygon, clipPolygon) |
||
|> Enum.each(&IO.inspect/1)</ |
|> Enum.each(&IO.inspect/1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,208: | Line 1,208: | ||
The polygons are fortran type with an allocatable array "vertex" that contains the vertices and an integer n that is the size of the polygon. For any polygon, the first vertex and the last vertex have to be the same. |
The polygons are fortran type with an allocatable array "vertex" that contains the vertices and an integer n that is the size of the polygon. For any polygon, the first vertex and the last vertex have to be the same. |
||
As you will see, in the main function, we allocate the vertex array of the result polygon with its maximal size. |
As you will see, in the main function, we allocate the vertex array of the result polygon with its maximal size. |
||
<syntaxhighlight lang="fortran"> |
|||
<lang Fortran> |
|||
module SutherlandHodgmanUtil |
module SutherlandHodgmanUtil |
||
Line 1,468: | Line 1,468: | ||
end program main |
end program main |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Suterland-Hodgman |
Suterland-Hodgman |
||
Line 1,488: | Line 1,488: | ||
FreeBASIC has inbuilt gfx graphics (a main feature), but I have no access to graphics uploads. |
FreeBASIC has inbuilt gfx graphics (a main feature), but I have no access to graphics uploads. |
||
So no extra credits. |
So no extra credits. |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Type Point |
Type Point |
||
As Double x,y |
As Double x,y |
||
Line 1,540: | Line 1,540: | ||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,558: | Line 1,558: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
No extra credit today. |
No extra credit today. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,606: | Line 1,606: | ||
} |
} |
||
fmt.Println(outputList) |
fmt.Println(outputList) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,614: | Line 1,614: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">module SuthHodgClip (clipTo) where |
||
import Data.List |
import Data.List |
||
Line 1,664: | Line 1,664: | ||
let targPoly = polyFrom targPts |
let targPoly = polyFrom targPts |
||
clipLines = linesFrom (polyFrom clipPts) |
clipLines = linesFrom (polyFrom clipPts) |
||
in foldl' (<|) targPoly clipLines</ |
in foldl' (<|) targPoly clipLines</syntaxhighlight> |
||
Print the resulting list of points and display the polygons in a window.<br> |
Print the resulting list of points and display the polygons in a window.<br> |
||
< |
<syntaxhighlight lang="haskell">import Graphics.HGL |
||
import SuthHodgClip |
import SuthHodgClip |
||
Line 1,696: | Line 1,696: | ||
drawLines w penP clipPts |
drawLines w penP clipPts |
||
drawSolid w green $ toInts resPts |
drawSolid w green $ toInts resPts |
||
getKey w</ |
getKey w</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,705: | Line 1,705: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">NB. assumes counterclockwise orientation. |
||
NB. determine whether point y is inside edge x. |
NB. determine whether point y is inside edge x. |
||
isinside=:0< [:-/ .* {.@[ -~"1 {:@[,:] |
isinside=:0< [:-/ .* {.@[ -~"1 {:@[,:] |
||
Line 1,729: | Line 1,729: | ||
end. |
end. |
||
subject |
subject |
||
)</ |
)</syntaxhighlight> |
||
{{out|Example use}} |
{{out|Example use}} |
||
< |
<syntaxhighlight lang="j"> subject=: 50 150,200 50,350 150,350 300,250 300,200 250,150 350,100 250,:100 200 |
||
clip=: 100 100,300 100,300 300,:100 300 |
clip=: 100 100,300 100,300 300,:100 300 |
||
clip SutherlandHodgman subject |
clip SutherlandHodgman subject |
||
Line 1,743: | Line 1,743: | ||
175 300 |
175 300 |
||
125 300 |
125 300 |
||
100 250</ |
100 250</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|7}} |
{{works with|Java|7}} |
||
< |
<syntaxhighlight lang="java5">import java.awt.*; |
||
import java.awt.geom.Line2D; |
import java.awt.geom.Line2D; |
||
import java.util.*; |
import java.util.*; |
||
Line 1,864: | Line 1,864: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="javascript"> |
||
<html> |
<html> |
||
<head> |
<head> |
||
Line 1,938: | Line 1,938: | ||
</body> |
</body> |
||
</html> |
</html> |
||
</syntaxhighlight> |
|||
</lang> |
|||
You can see it running <code>[http://jsfiddle.net/elisherer/y6RDB/ here]</code> |
You can see it running <code>[http://jsfiddle.net/elisherer/y6RDB/ here]</code> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Luxor |
||
isinside(p, a, b) = (b.x - a.x) * (p.y - a.y) > (b.y - a.y) * (p.x - a.x) |
isinside(p, a, b) = (b.x - a.x) * (p.y - a.y) > (b.y - a.y) * (p.x - a.x) |
||
Line 1,996: | Line 1,996: | ||
preview() |
preview() |
||
println(clipped) |
println(clipped) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Point[Point(100.0, 116.667), Point(125.0, 100.0), Point(275.0, 100.0), Point(300.0, 116.667), |
Point[Point(100.0, 116.667), Point(125.0, 100.0), Point(275.0, 100.0), Point(300.0, 116.667), |
||
Line 2,005: | Line 2,005: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.awt.* |
import java.awt.* |
||
Line 2,109: | Line 2,109: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
No extra credit. |
No extra credit. |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="lua">subjectPolygon = { |
||
{50, 150}, {200, 50}, {350, 150}, {350, 300}, |
{50, 150}, {200, 50}, {350, 150}, {350, 300}, |
||
{250, 300}, {200, 250}, {150, 350}, {100, 250}, {100, 200} |
{250, 300}, {200, 250}, {150, 350}, {100, 250}, {100, 200} |
||
Line 2,175: | Line 2,175: | ||
end |
end |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="lua">{100.000000, 116.666667}, |
||
{125.000000, 100.000000}, |
{125.000000, 100.000000}, |
||
{275.000000, 100.000000}, |
{275.000000, 100.000000}, |
||
Line 2,186: | Line 2,186: | ||
{175.000000, 300.000000}, |
{175.000000, 300.000000}, |
||
{125.000000, 300.000000}, |
{125.000000, 300.000000}, |
||
{100.000000, 250.000000},</ |
{100.000000, 250.000000},</syntaxhighlight> |
||
(You can also [http://ideone.com/5tGEQ see it live]) |
(You can also [http://ideone.com/5tGEQ see it live]) |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
Geometry is built in to the Wolfram Language. |
Geometry is built in to the Wolfram Language. |
||
< |
<syntaxhighlight lang="mathematica">p1 = Polygon[{{50, 150}, {200, 50}, {350, 150}, {350, 300}, {250, 300}, {200, 250}, {150, 350}, {100, 250}, {100, 200}}]; |
||
p2 = Polygon[{{100, 100}, {300, 100}, {300, 300}, {100, 300}}]; |
p2 = Polygon[{{100, 100}, {300, 100}, {300, 300}, {100, 300}}]; |
||
RegionIntersection[p1, p2] |
RegionIntersection[p1, p2] |
||
Graphics[{Red, p1, Blue, p2, Green, RegionIntersection[p1, p2]}]</ |
Graphics[{Red, p1, Blue, p2, Green, RegionIntersection[p1, p2]}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Polygon[{{125, 100}, {100, 350/3}, {100, 200}, {100, 250}, {125, 300}, {175, 300}, {200, 250}, {250, 300}, {300, 300}, {300, 350/3}, {275, 100}}]</pre> |
<pre>Polygon[{{125, 100}, {100, 350/3}, {100, 200}, {100, 250}, {125, 300}, {175, 300}, {200, 250}, {250, 300}, {300, 300}, {300, 350/3}, {275, 100}}]</pre> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">%The inputs are a table of x-y pairs for the verticies of the subject |
||
%polygon and boundary polygon. (x values in column 1 and y values in column |
%polygon and boundary polygon. (x values in column 1 and y values in column |
||
%2) The output is a table of x-y pairs for the clipped version of the |
%2) The output is a table of x-y pairs for the clipped version of the |
||
Line 2,285: | Line 2,285: | ||
end %for subject verticies |
end %for subject verticies |
||
end %for boundary verticies |
end %for boundary verticies |
||
end %sutherlandHodgman</ |
end %sutherlandHodgman</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="matlab">>> subject = [[50;200;350;350;250;200;150;100;100],[150;50;150;300;300;250;350;250;200]]; |
||
>> clipPolygon = [[100;300;300;100],[100;100;300;300]]; |
>> clipPolygon = [[100;300;300;100],[100;100;300;300]]; |
||
>> clippedSubject = sutherlandHodgman(subject,clipPolygon); |
>> clippedSubject = sutherlandHodgman(subject,clipPolygon); |
||
Line 2,294: | Line 2,294: | ||
>> plot([clipPolygon(:,1);clipPolygon(1,1)],[clipPolygon(:,2);clipPolygon(1,2)],'r') |
>> plot([clipPolygon(:,1);clipPolygon(1,1)],[clipPolygon(:,2);clipPolygon(1,2)],'r') |
||
>> patch(clippedSubject(:,1),clippedSubject(:,2),0); |
>> patch(clippedSubject(:,1),clippedSubject(:,2),0); |
||
>> axis square</ |
>> axis square</syntaxhighlight> |
||
[[File:Sutherland-Hodgman_MATLAB.png]] |
[[File:Sutherland-Hodgman_MATLAB.png]] |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="nim">import sequtils, strformat |
||
type |
type |
||
Line 2,383: | Line 2,383: | ||
for point in clipped: |
for point in clipped: |
||
echo &"({point.x:.3f}, {point.y:.3f})" |
echo &"({point.x:.3f}, {point.y:.3f})" |
||
saveEpsImage("sutherland_hodgman_clipping_out.eps", subjectPolygon, clippingPolygon, clipped)</ |
saveEpsImage("sutherland_hodgman_clipping_out.eps", subjectPolygon, clippingPolygon, clipped)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,399: | Line 2,399: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let is_inside (x,y) ((ax,ay), (bx,by)) = |
||
(bx -. ax) *. (y -. ay) > (by -. ay) *. (x -. ax) |
(bx -. ax) *. (y -. ay) > (by -. ay) *. (x -. ax) |
||
Line 2,449: | Line 2,449: | ||
List.iter (fun (x,y) -> |
List.iter (fun (x,y) -> |
||
Printf.printf " (%g, %g)\n" x y; |
Printf.printf " (%g, %g)\n" x y; |
||
) (poly_clip subject_polygon clip_polygon)</ |
) (poly_clip subject_polygon clip_polygon)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> (100, 116.667) |
<pre> (100, 116.667) |
||
Line 2,462: | Line 2,462: | ||
(100, 250)</pre> |
(100, 250)</pre> |
||
We can display the result in a window using the <code>[https://v2.ocaml.org/releases/4.08/htmlman/libref/Graphics.html Graphics]</code> module: |
We can display the result in a window using the <code>[https://v2.ocaml.org/releases/4.08/htmlman/libref/Graphics.html Graphics]</code> module: |
||
< |
<syntaxhighlight lang="ocaml">let subject_polygon = |
||
[ ( 50.0, 150.0); (200.0, 50.0); (350.0, 150.0); |
[ ( 50.0, 150.0); (200.0, 50.0); (350.0, 150.0); |
||
(350.0, 300.0); (250.0, 300.0); (200.0, 250.0); |
(350.0, 300.0); (250.0, 300.0); (200.0, 250.0); |
||
Line 2,489: | Line 2,489: | ||
draw_poly Graphics.magenta Graphics.blue (poly_clip subject_polygon clip_polygon); |
draw_poly Graphics.magenta Graphics.blue (poly_clip subject_polygon clip_polygon); |
||
let _ = Graphics.wait_next_event [Graphics.Button_down; Graphics.Key_pressed] in |
let _ = Graphics.wait_next_event [Graphics.Button_down; Graphics.Key_pressed] in |
||
Graphics.close_graph ()</ |
Graphics.close_graph ()</syntaxhighlight> |
||
[[File:SuthHodgClip_OCaml.png]] |
[[File:SuthHodgClip_OCaml.png]] |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
Line 2,543: | Line 2,543: | ||
print "Clipped polygon:\n"; |
print "Clipped polygon:\n"; |
||
print '(' . join(' ', @$_) . ') ' for @clipped;</ |
print '(' . join(' ', @$_) . ') ' for @clipped;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Clipped polygon: |
<pre>Clipped polygon: |
||
Line 2,552: | Line 2,552: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/shpc.htm here]. |
You can run this online [http://phix.x10.mx/p2js/shpc.htm here]. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\Sutherland_Hodgman_polygon_clipping.exw |
-- demo\rosetta\Sutherland_Hodgman_polygon_clipping.exw |
||
Line 2,655: | Line 2,655: | ||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"> |
||
<?php |
<?php |
||
function clip ($subjectPolygon, $clipPolygon) { |
function clip ($subjectPolygon, $clipPolygon) { |
||
Line 2,706: | Line 2,706: | ||
echo "\n"; |
echo "\n"; |
||
?> |
?> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="purebasic">Structure point_f |
||
x.f |
x.f |
||
y.f |
y.f |
||
Line 2,793: | Line 2,793: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(100.00, 116.67) |
<pre>(100.00, 116.67) |
||
Line 2,807: | Line 2,807: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
def clip(subjectPolygon, clipPolygon): |
def clip(subjectPolygon, clipPolygon): |
||
def inside(p): |
def inside(p): |
||
Line 2,840: | Line 2,840: | ||
cp1 = cp2 |
cp1 = cp2 |
||
return(outputList) |
return(outputList) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Shameless rewrite of haskell version. |
Shameless rewrite of haskell version. |
||
< |
<syntaxhighlight lang="scheme">#lang racket |
||
(module sutherland-hodgman racket |
(module sutherland-hodgman racket |
||
Line 2,901: | Line 2,901: | ||
(define (clip-to sp-pts cp-edges) |
(define (clip-to sp-pts cp-edges) |
||
(for/fold ([out-poly sp-pts]) ([clip-line cp-edges]) |
(for/fold ([out-poly sp-pts]) ([clip-line cp-edges]) |
||
(isect-polygon (make-edges out-poly) clip-line)))) </ |
(isect-polygon (make-edges out-poly) clip-line)))) </syntaxhighlight> |
||
---- |
---- |
||
Testing code (Couldn't find a way to attach image with polygons) |
Testing code (Couldn't find a way to attach image with polygons) |
||
< |
<syntaxhighlight lang="scheme">(require racket/gui) |
||
(require 'sutherland-hodgman) |
(require 'sutherland-hodgman) |
||
Line 2,955: | Line 2,955: | ||
clipped-poly)) |
clipped-poly)) |
||
(run)</ |
(run)</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="scheme">(list |
||
(point 300 300) |
(point 300 300) |
||
(point 250 300) |
(point 250 300) |
||
Line 2,970: | Line 2,970: | ||
(point 125 100) |
(point 125 100) |
||
(point 275 100) |
(point 275 100) |
||
(point 300 350/3))</ |
(point 300 350/3))</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,977: | Line 2,977: | ||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub intersection ($L11, $L12, $L21, $L22) { |
||
my ($Δ1x, $Δ1y) = $L11 »-« $L12; |
my ($Δ1x, $Δ1y) = $L11 »-« $L12; |
||
my ($Δ2x, $Δ2y) = $L21 »-« $L22; |
my ($Δ2x, $Δ2y) = $L21 »-« $L22; |
||
Line 3,036: | Line 3,036: | ||
:polyline[ :points(@clipped.join: ','), :style<stroke:red>, :fill<red>, :opacity<.5> ], |
:polyline[ :points(@clipped.join: ','), :style<stroke:red>, :fill<red>, :opacity<.5> ], |
||
], |
], |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,045: | Line 3,045: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ruby">Point = Struct.new(:x,:y) do |
||
def to_s; "(#{x}, #{y})" end |
def to_s; "(#{x}, #{y})" end |
||
end |
end |
||
Line 3,091: | Line 3,091: | ||
clipPolygon = [[100, 100], [300, 100], [300, 300], [100, 300]].collect{|pnt| Point[*pnt]} |
clipPolygon = [[100, 100], [300, 100], [300, 300], [100, 300]].collect{|pnt| Point[*pnt]} |
||
puts sutherland_hodgman(subjectPolygon, clipPolygon)</ |
puts sutherland_hodgman(subjectPolygon, clipPolygon)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,108: | Line 3,108: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="rust">#[derive(Debug, Clone)] |
||
struct Point { |
struct Point { |
||
x: f64, |
x: f64, |
||
Line 3,173: | Line 3,173: | ||
let result = sutherland_hodgman_clip(&subject_polygon, &clip_polygon); |
let result = sutherland_hodgman_clip(&subject_polygon, &clip_polygon); |
||
println!("{:?}", result); |
println!("{:?}", result); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,184: | Line 3,184: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
From Java snippet. |
From Java snippet. |
||
< |
<syntaxhighlight lang="scala">import javax.swing.{ JFrame, JPanel } |
||
object SutherlandHodgman extends JFrame with App { |
object SutherlandHodgman extends JFrame with App { |
||
Line 3,273: | Line 3,273: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TypeScript}}== |
=={{header|TypeScript}}== |
||
< |
<syntaxhighlight lang="typescript">interface XYCoords { |
||
x : number; |
x : number; |
||
y : number; |
y : number; |
||
Line 3,332: | Line 3,332: | ||
} |
} |
||
return outputList |
return outputList |
||
}</ |
}</syntaxhighlight> |
||
Line 3,339: | Line 3,339: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">class Point(x, y) { |
||
method to_s { |
method to_s { |
||
"(#{'%.2f' % x}, #{'%.2f' % y})" |
"(#{'%.2f' % x}, #{'%.2f' % y})" |
||
Line 3,391: | Line 3,391: | ||
].map{|pnt| Point(pnt...) } |
].map{|pnt| Point(pnt...) } |
||
sutherland_hodgman(subjectPolygon, clipPolygon).each { .say }</ |
sutherland_hodgman(subjectPolygon, clipPolygon).each { .say }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,410: | Line 3,410: | ||
{{trans|Rust}} |
{{trans|Rust}} |
||
< |
<syntaxhighlight lang="swift">struct Point { |
||
var x: Double |
var x: Double |
||
var y: Double |
var y: Double |
||
Line 3,490: | Line 3,490: | ||
]) |
]) |
||
print(sutherlandHodgmanClip(subjPoly: subj, clipPoly: clip))</ |
print(sutherlandHodgmanClip(subjPoly: subj, clipPoly: clip))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,497: | Line 3,497: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl"># Find intersection of an arbitrary polygon with a convex one. |
||
package require Tcl 8.6 |
package require Tcl 8.6 |
||
Line 3,617: | Line 3,617: | ||
} |
} |
||
return $result |
return $result |
||
}</ |
}</syntaxhighlight> |
||
The specifics of the task: |
The specifics of the task: |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
< |
<syntaxhighlight lang="tcl">package require Tk |
||
grid [canvas .c -width 400 -height 400 -background \#ffffff] |
grid [canvas .c -width 400 -height 400 -background \#ffffff] |
||
Line 3,633: | Line 3,633: | ||
demonstrate {100 100 300 100 300 300 100 300} \ |
demonstrate {100 100 300 100 300 300 100 300} \ |
||
{50 150 200 50 350 150 350 300 250 300 200 250 150 350 100 250 100 200}</ |
{50 150 200 50 350 150 350 300 250 300 200 250 150 350 100 250 100 200}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,643: | Line 3,643: | ||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
{{libheader|Wren-polygon}} |
{{libheader|Wren-polygon}} |
||
< |
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color |
||
import "dome" for Window |
import "dome" for Window |
||
import "./polygon" for Polygon |
import "./polygon" for Polygon |
||
Line 3,722: | Line 3,722: | ||
[300, 300], [100, 300] |
[300, 300], [100, 300] |
||
] |
] |
||
var Game = SutherlandHodgman.new(600, 500, subject, clipper)</ |
var Game = SutherlandHodgman.new(600, 500, subject, clipper)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,741: | Line 3,741: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
<syntaxhighlight lang="yabasic"> |
|||
<lang Yabasic> |
|||
open window 400, 400 |
open window 400, 400 |
||
backcolor 0,0,0 |
backcolor 0,0,0 |
||
Line 3,844: | Line 3,844: | ||
sub round(n) |
sub round(n) |
||
return int(n + .5) |
return int(n + .5) |
||
end sub</ |
end sub</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C}}{{trans|Wikipedia}} |
{{trans|C}}{{trans|Wikipedia}} |
||
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
||
< |
<syntaxhighlight lang="zkl">class P{ // point |
||
fcn init(_x,_y){ var [const] x=_x.toFloat(), y=_y.toFloat() } |
fcn init(_x,_y){ var [const] x=_x.toFloat(), y=_y.toFloat() } |
||
fcn __opSub(p) { self(x - p.x, y - p.y) } |
fcn __opSub(p) { self(x - p.x, y - p.y) } |
||
Line 3,896: | Line 3,896: | ||
} |
} |
||
ppm.line(listOfPoints[0].ps.xplode(),listOfPoints[-1].ps.xplode(),rgb); |
ppm.line(listOfPoints[0].ps.xplode(),listOfPoints[-1].ps.xplode(),rgb); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">ppm:=PPM(400,400); |
||
clip:=T( P(100,100), P(300,100), P(300,300), P(100,300) ); |
clip:=T( P(100,100), P(300,100), P(300,300), P(100,300) ); |
||
polygon:=T( P( 50,150),P(200, 50),P(350,150), |
polygon:=T( P( 50,150),P(200, 50),P(350,150), |
||
Line 3,914: | Line 3,914: | ||
println("Clipped polygon has ",clipped.len()," points:"); |
println("Clipped polygon has ",clipped.len()," points:"); |
||
clipped.pump(Console.println);</ |
clipped.pump(Console.println);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Until local image uploading is re-enabled, see [http://www.zenkinetic.com/Images/RosettaCode/sutherland_hodgman.zkl.jpg this image]. |
Until local image uploading is re-enabled, see [http://www.zenkinetic.com/Images/RosettaCode/sutherland_hodgman.zkl.jpg this image]. |