# NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

## Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on April 22, 2010, 04:40:39 pm

 Title: how to detect a point is inside an area that is rotatable by cta ? Post by: lookang on April 22, 2010, 04:40:39 pm Hi everyone,i am able to detect xCompass,yCompass inside the rectangle area with centre xMagnet,yMagnet with sizex = Magnetsizex and sizey=Magnetsizey. Code:if ( xCompass<(xMagnet+Magnetsizex/2) && xCompass>(xMagnet-(Magnetsizex/2) && yCompass<(yMagnet+Magnetsizey/2) && yCompass>(yMagnet-Magnetsizey/2)){test = true; //  way to tell it is doing this b[0] = 225;b[1] = 225;}but when the rectangle area is rotatable by angle cta, i seems to be stuck trying to make the code detect the inside of the area rectangle.i tried it & probably it has something to do with transformation of the axes,but it feels rather long in the codeis there a way to do it elegantly? hmmmmmm  Title: Re: how to detect a point is inside an area that is rotatable by cta ? Post by: Fu-Kwun Hwang on April 22, 2010, 05:23:47 pm There are at least two ways to do it.1. Rotate the coordinate system so (x,y) become (x',y'), so it become zero degree again.And check it by the same method.2. if the coordinates for those four points are (x1,y1), (x2,y2), (x3,y3) and (x4,y4).Assume a line L1: \$y=m_1 x+b_1\$ pass (x1,y1) and (x2,y2). Then \$y>m_1a+b_1\$ is at the right side of the L1,  \$yAnother line L2: \$y=m_2 x+b_2\$ pass (x3,y3) and (x4,y4). If the compass is inside, it must be between L1 and L2.So you can find out condition (1) : \$(y-m_1 x-b_1)(y-m_2 x-b_2)<0 \$There are another two lines L3 :pass (x1,y1) , (x4,y4)L4: pass (x2,y2) , (x3,y3)So you can find out another condition (2): \$(y-m_3 x-b_3)(y-m_4 x-b_4)<0\$The compass is inside if the above two conditions are satisfied at the same time (use and operator ). Title: Re: how to detect a point is inside an area that is rotatable by cta ? Post by: lookang on April 22, 2010, 10:25:02 pm u r a genius!thanks!i used method oneCode:xMagnettransform = xMagnet*cs+yMagnet*sc;yMagnettransform = -xMagnet*sc+yMagnet*cs;xCompasstransform = xCompass*cs+yCompass*sc;yCompasstransform = -xCompass*sc+yCompass*cs;if ( magnetNS==false&&xCompasstransform<(xMagnettransform+Magnetsizex/2) && xCompasstransform>(xMagnettransform-(Magnetsizex/2)) && yCompasstransform<(yMagnettransform+Magnetsizey/2) && yCompasstransform>(yMagnettransform-Magnetsizey/2)){test = true;b[0] = (225*cs)*vectorfieldmaxopposite/100;b[1] = (225*sc)*vectorfieldmaxopposite/100;}it works so beautifully now. ;D alright man super!!crap, introduce some other bugs, gotta look at it closer again haiz