問(wèn)題描述
我遇到了麻煩.我需要使用拖動(dòng)偵聽(tīng)器和單擊偵聽(tīng)器圍繞其中心旋轉(zhuǎn)等邊三角形.三角形應(yīng)該會(huì)增長(zhǎng),但現(xiàn)在會(huì)改變角度并旋轉(zhuǎn)一個(gè)點(diǎn),同時(shí)以三角形的中間為中心.這是我的問(wèn)題,它目前正在拖動(dòng)點(diǎn) 3 并圍繞點(diǎn) 1 旋轉(zhuǎn).我有一個(gè)值 x 和 y 的數(shù)組,它存儲(chǔ) 4 個(gè)值,每個(gè)值首先在序數(shù)值 0 處包含初始點(diǎn),在點(diǎn) 1 2 和 3 處對(duì)應(yīng)的值.
I am having trouble. I need to rotate an equilateral triangle around it's centre by using the drag listener and click listener. The triangle should grow but now change angles and be rotated by a point while being centred at the middle of the triangle. This is my problem, it is currently dragging by the point 3 and rotating around point 1. I have an array of values x and y and it stores 4 values each containing the initial point first at ordinal value 0 and point 1 2 and 3 at the corresponding values.
`
我的問(wèn)題是這段代碼基本上運(yùn)行正確,但我被告知旋轉(zhuǎn)的頂點(diǎn)必須在三角形的中間.我的是第一點(diǎn).
My issue is that this code basically runs correctly but I am told the vertex point of rotation has to be in the middle of the triangle. Mine is the first point.
推薦答案
先看看 2D 圖形,特別是 變換形狀、文本、和圖片.
基本上,您的多邊形"將具有可定義的大小(最大 x/y 點(diǎn)),由此,您可以確定多邊形"的中心位置,例如...
Basically, your "polygon" will have a definable size (the maximum x/y point), from this, you can determine the center position of the "polygon", for example...
這只是返回多邊形的最大 x 和 y 邊界.這允許您計(jì)算多邊形的中心位置.您馬上就會(huì)明白為什么您不需要實(shí)際指定原點(diǎn)...
This just returns the maximum x and y bounds of your polygon. This allows you to calculate the center position of the polygon. You'll see why in a second why you don't need to actually specify the origin point...
接下來(lái),我們計(jì)算一個(gè)AffineTransform
,就是直接應(yīng)用到Graphics
上下文...
Next, we calculate a AffineTransform
, which is the applied to the Graphics
context directly...
這不僅會(huì)平移三角形位置,還會(huì)旋轉(zhuǎn)它.這意味著您可以創(chuàng)建一個(gè)標(biāo)準(zhǔn)化多邊形(其原點(diǎn)為 0x0)并允許 Graphics
上下文將其放置在您想要的位置,這讓生活變得更加輕松......
This not only translates the triangle position, but will also rotate it. What this means you can create a normalised polygon (whose origin point is 0x0) and allow the Graphics
context to place it where you want it, this makes life SO much easier...
現(xiàn)在,旋轉(zhuǎn)計(jì)算是基于計(jì)算兩點(diǎn)之間的角度,點(diǎn)擊"點(diǎn)和拖動(dòng)"點(diǎn)...
Now, the rotation calculation is based on calculating the angle between two points, the "click" point and the "drag" point...
這基于 this question
例如...
紅線是一個(gè)簡(jiǎn)單的指南,表明三角形的尖端指向鼠標(biāo)...
The red line is simple a guide to show that the tip of the triangle is point towards the mouse...
現(xiàn)在,在你跳到我頭上抱怨解決方案太復(fù)雜"之前,請(qǐng)理解我是個(gè)白癡,說(shuō)真的,我 2 歲的孩子對(duì)基礎(chǔ)數(shù)學(xué)的掌握比我還好,這就是我能想出的最簡(jiǎn)單的解決方案不會(huì)融化我的大腦并使用雙陣列多邊形 API.就個(gè)人而言,我會(huì)使用 Shape
API,但這不是你開(kāi)始使用的......
Now, before you jump all over me and complain that the solution is "too complex", understand that I'm an idiot, seriously, my 2 year old has a better grasp on basic mathematics then I do, this is the most simplistic solution I can come up with that doesn't melt my brain and uses the dual array polygon API. Personally, I'd use the Shape
API, but that's not what you started with...
這篇關(guān)于圍繞點(diǎn)旋轉(zhuǎn)三角形java的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!