PointF 90degrees from X

旧巷老猫 提交于 2019-12-13 03:48:41

问题


Yesterday I asked a question - Draw arrow according to path

In that question I explained that I want to draw a arrow head in the direction of my onTouchEvent. I got a answer in the comments saying I should rotate the canvas.

Last night I got a new idea and this is where this question comes in.


I get the x position/coordinate in MotionEvent.ACTION_DOWN by calling event.getX(); inside my onTouchEvent.

I would like to know if it is possible to get a point/coordinate 90 degrees from x?

Here is a demonstration to give move clarity:

                                      |
                                      |
                                      |
                                      |
                                      |
                                      |
point I want to get --> x1------------x

As shown above, x1 is what I want to get.


Also worth mentioning, the following will not work because the x axis might be at a angle:

x1 = x - value

EDIT:

I think I made the question unclear, sorry about that.

I'm going to create a scenario to explain myself better

In my onTouchEvent inside MotionEvent.ACTION_DOWN I set the starting x and y, lets call it xdown and ydown, by calling event.getX() and event.getY().

Then inside MotionEvent.ACTION_UP I get the x and y, same as above, lets call it xup and yup.

I then get the center point, lets call it centerx and centery.

It will now look like this:

               xdown, ydown
              |
              |
              |
              |
              |
              |
              |centerx, centery
              |
              |
              |
              |
              |
              |
               xup, yup

Now I want to get the x and y 90 degrees from centerx, centery, lets say at a distance of 40.

Another demonstaraiton:

                              xdown, ydown
                             |
                             |
                             |
                             |
                             |
                             |
  nintyX,nintyY <------------|centerx, centery
                             |
                             |
                             |
                             |
                             |
                             |
                              xup, yup

回答1:


Seems you have some direction and want to get perpendicular direction.

Components of direction vectors are (dx, dy)

dx = centerx - xup
dy = centery - yup

then components of left (note - "leftness" depends on coordinates system handness!) perpendicular vector are

 px =  - dy
 py =   dx 

(px=dy and py=-dx if direction is wrong for you)

To get a point ninty at distance D in needed direction, normalize vector and multiply its components by D

lenp = sqrt(px*px + py*py)  
upx = px / lenp
upy = py / lenp
nintyX = centerx + upx * D 
nintyY = centery + upy * D 


来源:https://stackoverflow.com/questions/53513066/pointf-90degrees-from-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!