comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Polygon Clipping Algo in IDL
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Polygon Clipping Algo in IDL [message #46720] Tue, 13 December 2005 15:14
Mark Hadfield is currently offline  Mark Hadfield
Messages: 783
Registered: May 1995
Senior Member
raval.chintan@gmail.com wrote:
> Dear Mark,
>
> How can i give two polygon as an input to your program.
> Suppose i have polygon 1 ) [[0,0],[0,3],[3,3],[3,0]]
> 2] [[1,1][1,4],[4,4],[4,1]]
>
> Here my polygons are rectangle. First polygon contains upper left
> point as [0,0] and lower right as [3,3].
> and second polygon contains upper left as [1,1] and lower right as
> [4,4] .
>
> Now my result (output ) polygon will contain [1,1] as upper left and
> [3,3] as lower right .
>
> Regards
> Chintan
>

Ok. Let's (arbitrarily) consider the first polygon as the one to be
clipped (the clippee) and the second as the one to clip to (the
clipper). MGH_POLYCLIP clips a polygon to a line, so we will need to
apply it 4 times. It turns out that I have a function that does this,
called MGH_POLYBOX, attached. It's not in the Motley library but it
probably should be.

The calling sequence is

result = MGH_POLYBOX(xclip, yclip, polin)

where xclip is a 2-element vector specifying the clipping values in the
X direction, yclip is a 2-element vector specifying the clipping values
in the Y direction and polin is a [2,n] vector defining the polygon to
be clipped.

So in your case

IDL> xclip = [1,4]
IDL> yclip = [1,4]
IDL> polin = [[0,0],[0,3],[3,3],[3,0]]
IDL> print, mgh_polybox(xclip, yclip, polin)
1 1
1 3
3 3
3 1

--
Mark Hadfield "Kei puwaha te tai nei, Hoea tahi tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)


;+
; NAME:
; MGH_POLYBOX
;
; PURPOSE:
; Clip an arbitrary polygon on the X-Y plane to a box (a rectangle
; defined by X and Y limits) using the Sutherland-Hodgman algorithm.
;
; CATEGORY:
; Graphics, Region of Interest, Geometry
;
; CALLING SEQUENCE:
; result = MGH_POLYBOX(xclip, yclip, polin, COUNT=count)
;
; RETURN VALUE
; The function returns [2,n] vector defining the clipped polygon. The
; second dimension will equal the value of the COUNT argument, except
; where this is 0 in which the return value is -1.
;
; ARGUMENTS
; xclip A 2-element vector specifying the clipping values in the
; X direction
;
; yclip A 2-element vector specifying the clipping values in the
; Y direction
;
; polin A [2,n] vector defining the polygon to be clipped.
;
; KEYWORDS
; COUNT Associate this keyword with a named variable to return
; the number of vertices in the clipped polygon.
;
; PROCEDURE:
; The polygon is clipped to each edge in turn using the Sutherland-Hodgman
; algorithm.
;
; This function is based on JD Smith's POLYCLIP function. He can take all
; of the credit and none of the blame.
;
; MODIFICATION HISTORY:
; Mark Hadfield, 2001-10:
; I wrote thsi first as a stand-alone function, based on JD Smith's
; POLYCLIP, then modified it so that it just calls MGH_POLYCLIP
; up to 4 times.
;-

function mgh_polybox, xc, yc, polin, COUNT=count

compile_opt DEFINT32
compile_opt STRICTARR

polout = mgh_polyclip(xc[0], 0B, 0B, polin, COUNT=count)

if count eq 0 then return, polout

polout = mgh_polyclip(xc[1], 0B, 1B,polout, COUNT=count)

if count eq 0 then return, polout

polout = mgh_polyclip(yc[0], 1B, 0B, polout, COUNT=count)

if count eq 0 then return, polout

polout = mgh_polyclip(yc[1], 1B, 1B, polout, COUNT=count)

return, polout

end
Re: Polygon Clipping Algo in IDL [message #46728 is a reply to message #46720] Mon, 12 December 2005 22:32 Go to previous message
raval.chintan is currently offline  raval.chintan
Messages: 54
Registered: May 2005
Member
Dear Mark,

How can i give two polygon as an input to your program.
Suppose i have polygon 1 ) [[0,0],[0,3],[3,3],[3,0]]
2] [[1,1][1,4],[4,4],[4,1]]

Here my polygons are rectangle. First polygon contains upper left
point as [0,0] and lower right as [3,3].
and second polygon contains upper left as [1,1] and lower right as
[4,4] .

Now my result (output ) polygon will contain [1,1] as upper left and
[3,3] as lower right .

Regards
Chintan
Re: Polygon Clipping Algo in IDL [message #46729 is a reply to message #46728] Mon, 12 December 2005 21:29 Go to previous message
raval.chintan is currently offline  raval.chintan
Messages: 54
Registered: May 2005
Member
Dear Mark,

Thank You.

Regards,
Chintan Raval.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Saving more than one image in a widget
Next Topic: Re: Fun Writing Books

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 22:18:25 PDT 2025

Total time taken to generate the page: 0.15941 seconds