In a triangulated isometric grid, which triangle is a given point?

I have a triangulated isometric grid, for example: (source: mathforum.org )alt text

In my code, triangles are grouped by columns.

When hovering over the mouse, I want to calculate in which triangle the coordinates of the mouse are. Is there a simple algorithm for this?

+5
source share
2 answers

What you want to do is turn this into a grid as much as possible, because grids are much easier to work with.

, , , . , , , x . .

, (). , , .

y ( ). , . , :

+--------+
|-_      |
|  -_    |
|    -_  |
|      -_|
+--------+

. , , , , .

.

  • - Bresenham, , , ;
  • ( , - ). , , 3 2, 6 4 ..

(1) (2) .

, , , , ?

+4

, cletus, - , .

, 1.

, , :

       y'
      /
     /__/__/__/__/__/__/
    /__/__/__/__/__/__/
   /__/__/__/__/__/__/____ x'
(0,0)

, x y 60 , (x ', y') ( - ) (x, y).

(x, y), (x ', y'), .

- x j, y, ,

x'* i + y'( i/2 + sqrt(3) * j /2) = xi + yj.

( "" y i/2 + sqrt (3)/2 * j. x x, .. i).

,

x' + y'/2 = x
y' * sqrt(3)/2 = y

:

y' = 2*y/sqrt(3)
x' = x - y/sqrt(3)

, x ' y' .

, c = [x '], x'

r = [y '], y'

(angular) c- r- . ( 0).

,

       ____
      /\ * /   
     /___\/
   (c,r)

, , , x ' y'.

{x} = x' - [x'] = x' - c.
{y} = y' - [y'] = y' - r.

,

if {x} + {y} > 1, , *. {x} + {y} < 1, . {x} + {y} = 1, , .

, .

+3

All Articles