[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

comparing complex numbers with other numbers

Bigos

5/14/2016 9:14:00 PM

I try compare vectors to see if a polygon is convex.

My approach tries to measure angles of polygon and check if the angle
goes beyond 180 degrees. Current method of calculating vector angles
does not provide this information. So I thought I'll rotate on of the
sides 90 counter clockwise and see if the new angle is less than 90
degrees, giving me chance to see if the angle goes beyond 180 degrees
counter clockwise.

That lead to my question. When the angle of v1-rot-and-v2 is 90 or very
close I get a simple-type error. To go around it I have devised
following code.

(if (realp v1-rot-and-v2)
(if (< v1-rot-and-v2 90)
'less
'more)
'hmm))

Will I get the problem only with 90 degrees? What is the recomended way
of handling these errors?
7 Answers

Bigos

5/14/2016 9:30:00 PM

0

On 14/05/16 22:14, Bigos wrote:
> I try compare vectors to see if a polygon is convex.
>
> My approach tries to measure angles of polygon and check if the angle
> goes beyond 180 degrees. Current method of calculating vector angles
> does not provide this information. So I thought I'll rotate on of the
> sides 90 counter clockwise and see if the new angle is less than 90
> degrees, giving me chance to see if the angle goes beyond 180 degrees
> counter clockwise.
>
> That lead to my question. When the angle of v1-rot-and-v2 is 90 or very
> close I get a simple-type error. To go around it I have devised
> following code.
>
> (if (realp v1-rot-and-v2)
> (if (< v1-rot-and-v2 90)
> 'less
> 'more)
> 'hmm))
>
> Will I get the problem only with 90 degrees? What is the recomended way
> of handling these errors?

following seems to work

(if (< (if (not (realp v1-rot-and-v2))
(realpart v1-rot-and-v2)
v1-rot-and-v2)
90)
'less
'more)

Barry Margolin

5/15/2016 12:25:00 AM

0

In article <nh85dh$kap$1@gioia.aioe.org>,
Bigos <ruby.object@googlemail.com> wrote:

> On 14/05/16 22:14, Bigos wrote:
> > I try compare vectors to see if a polygon is convex.
> >
> > My approach tries to measure angles of polygon and check if the angle
> > goes beyond 180 degrees. Current method of calculating vector angles
> > does not provide this information. So I thought I'll rotate on of the
> > sides 90 counter clockwise and see if the new angle is less than 90
> > degrees, giving me chance to see if the angle goes beyond 180 degrees
> > counter clockwise.
> >
> > That lead to my question. When the angle of v1-rot-and-v2 is 90 or very
> > close I get a simple-type error. To go around it I have devised
> > following code.
> >
> > (if (realp v1-rot-and-v2)
> > (if (< v1-rot-and-v2 90)
> > 'less
> > 'more)
> > 'hmm))
> >
> > Will I get the problem only with 90 degrees? What is the recomended way
> > of handling these errors?
>
> following seems to work
>
> (if (< (if (not (realp v1-rot-and-v2))
> (realpart v1-rot-and-v2)
> v1-rot-and-v2)
> 90)
> 'less
> 'more)

You don't need the IF. You can call REALPART on a REAL, and it will just
return it unchanged. And you can call IMAGPART and it will return a zero
of the same type.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Bigos

5/15/2016 2:34:00 AM

0

On 15/05/16 01:25, Barry Margolin wrote:

> You don't need the IF. You can call REALPART on a REAL, and it will just
> return it unchanged. And you can call IMAGPART and it will return a zero
> of the same type.
>
thank you

Kaz Kylheku

5/15/2016 5:05:00 AM

0

On 2016-05-14, Bigos <ruby.object@googlemail.com> wrote:
> I try compare vectors to see if a polygon is convex.
>
> My approach tries to measure angles of polygon and check if the angle
> goes beyond 180 degrees. Current method of calculating vector angles
> does not provide this information.

You don't need to calculate the angle. Treat the polygon as a directed
path, whose edges are vectors. You can take the cross product of two
successive vectors; its sign tells you whether the next edge turns left
or right relative to the previous one.

George Neuner

5/15/2016 4:53:00 PM

0

On Sun, 15 May 2016 05:05:10 +0000 (UTC), Kaz Kylheku
<545-066-4921@kylheku.com> wrote:

>On 2016-05-14, Bigos <ruby.object@googlemail.com> wrote:
>> I try compare vectors to see if a polygon is convex.
>>
>> My approach tries to measure angles of polygon and check if the angle
>> goes beyond 180 degrees. Current method of calculating vector angles
>> does not provide this information.
>
>You don't need to calculate the angle. Treat the polygon as a directed
>path, whose edges are vectors. You can take the cross product of two
>successive vectors; its sign tells you whether the next edge turns left
>or right relative to the previous one.

I think you meant 'dot product' ... the cross product is area.

George

Icarus Sparry

5/15/2016 7:44:00 PM

0

On Sun, 15 May 2016 12:53:22 -0400, George Neuner wrote:

> On Sun, 15 May 2016 05:05:10 +0000 (UTC), Kaz Kylheku
> <545-066-4921@kylheku.com> wrote:
>
>>On 2016-05-14, Bigos <ruby.object@googlemail.com> wrote:
>>> I try compare vectors to see if a polygon is convex.
>>>
>>> My approach tries to measure angles of polygon and check if the angle
>>> goes beyond 180 degrees. Current method of calculating vector angles
>>> does not provide this information.
>>
>>You don't need to calculate the angle. Treat the polygon as a directed
>>path, whose edges are vectors. You can take the cross product of two
>>successive vectors; its sign tells you whether the next edge turns left
>>or right relative to the previous one.
>
> I think you meant 'dot product' ... the cross product is area.
>
> George

I'm with Kaz.

One way of looking at the dot product of 2 vectors ('u' and 'v' in the
conventions I learned) is that it is the magnitude ('length') of u times
the magnitude of v * the cosine of the angle between them. Since we only
care if the value is positive, negative or zero, and we can ignore zero
length vectors, this amounts to just looking at the cosine function.
Since the cosine function is non negative in the range -90 degrees to 90
degrees, this doesn't help determine if the second vector is "left" or
"right" relative to the first.

On the other hand, the cross product of two vectors is a new vector at 90
degrees to both vectors with magnitude = magnitude u * magnitude v * the
sine of the angle between them. The ambiguity between the two possible
directions is resolved by the "right hand rule". Again just being
interested in the sign of the result, and ignoring zero sized vectors,
this is the sign of the sine function. It is positive if the angle is
between 0 and 180 degrees, and negative between -180 and -0 degrees. In
other words it can tell left from right (whilst cosine can tell forward
from backward)


So if the OP's polygon is restricted to 2 dimensions, we can say the
given two vectors [u1,u2,0] and [v1,v2,0] their cross product is
[0, 0, u1*v2-u2*v1]. So the OP just needs to work out u1*v2-u2*v1 for
each successive pair of vectors, and ensure that the values are either
all positive or all negative, or a mixture of positive and negative. If
there is a mixture then the polygon is not convex, otherwise it is.

Notes.
[1] If there are any zero values then two edges are linear. Probably in
terms of "convex" the zero values can be ignored unless they are all
zero, but it depends on the exact requirements.


Jeff Barnett

5/16/2016 12:27:00 AM

0

Icarus Sparry wrote on 5/15/2016 1:43 PM:
> On Sun, 15 May 2016 12:53:22 -0400, George Neuner wrote:
>
>> On Sun, 15 May 2016 05:05:10 +0000 (UTC), Kaz Kylheku
>> <545-066-4921@kylheku.com> wrote:
>>
>>> On 2016-05-14, Bigos <ruby.object@googlemail.com> wrote:
>>>> I try compare vectors to see if a polygon is convex.
>>>>
>>>> My approach tries to measure angles of polygon and check if the angle
>>>> goes beyond 180 degrees. Current method of calculating vector angles
>>>> does not provide this information.
>>>
>>> You don't need to calculate the angle. Treat the polygon as a directed
>>> path, whose edges are vectors. You can take the cross product of two
>>> successive vectors; its sign tells you whether the next edge turns left
>>> or right relative to the previous one.
>>
>> I think you meant 'dot product' ... the cross product is area.
>>
>> George
>
> I'm with Kaz.
>
> One way of looking at the dot product of 2 vectors ('u' and 'v' in the
> conventions I learned) is that it is the magnitude ('length') of u times
> the magnitude of v * the cosine of the angle between them. Since we only
> care if the value is positive, negative or zero, and we can ignore zero
> length vectors, this amounts to just looking at the cosine function.
> Since the cosine function is non negative in the range -90 degrees to 90
> degrees, this doesn't help determine if the second vector is "left" or
> "right" relative to the first.
>
> On the other hand, the cross product of two vectors is a new vector at 90
> degrees to both vectors with magnitude = magnitude u * magnitude v * the
> sine of the angle between them. The ambiguity between the two possible
> directions is resolved by the "right hand rule". Again just being
> interested in the sign of the result, and ignoring zero sized vectors,
> this is the sign of the sine function. It is positive if the angle is
> between 0 and 180 degrees, and negative between -180 and -0 degrees. In
> other words it can tell left from right (whilst cosine can tell forward
> from backward)
>
>
> So if the OP's polygon is restricted to 2 dimensions, we can say the
> given two vectors [u1,u2,0] and [v1,v2,0] their cross product is
> [0, 0, u1*v2-u2*v1]. So the OP just needs to work out u1*v2-u2*v1 for
> each successive pair of vectors, and ensure that the values are either
> all positive or all negative, or a mixture of positive and negative. If
> there is a mixture then the polygon is not convex, otherwise it is.
>
> Notes.
> [1] If there are any zero values then two edges are linear. Probably in
> terms of "convex" the zero values can be ignored unless they are all
> zero, but it depends on the exact requirements.

A much shorter version of (part of) your argument to dismiss the dot
product from consideration is to note that it's value is the same if the
arguments are switched. The property to be determined depends on order
so the cross product (might!) help.
--
Jeff Barnett