在三角等距网格,什么三角形是一个给定的点在? [英] In a triangulated isometric grid, what triangle is a given point in?

查看:508
本文介绍了在三角等距网格,什么三角形是一个给定的点在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个等距三角格,像这样:

I have a triangulated isometric grid, like this:

在我的code,三角形按列进行分组。

In my code, triangles are grouped by columns.

在我的鼠标指针,我想计算一下三角形鼠标的坐标是,是否有一个简单的算法来做到这一点?

As I hover the mouse, I want to calculate what triangle the mouse coordinates are in. Is there a simple algorithm to do that?

推荐答案

您想要做的就是把它变成尽可能多的,因为网格是更容易的工作。一个网格

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

您要做的第一件事是什么工作列是。你说你存储所以应该做一个简单的整数除法就比较容易在x的列宽框起始位置的偏移坐标。很简单。

The first thing you do is work out what column it's in. You say you store that so it should be easier by doing a simple integer division on the x coordinate by the column width offset by the box start. Easy.

在要制定出什么三角形它在(明显)。你怎么一部分变成一个网格就是你pretend你有一堆直角的三角形,而不是一堆等距三角形。

After that you want to work out what triangle it's in (obviously). How you partially turn this into a grid is you pretend that you have a stack of right angle triangles instead of a stack of isometric triangles.

的三角形有沿y轴(塔的侧)的长度。除以这个数字在两个工作你到底是多少步下跌。根据步数下来,如果该列是偶数或奇数会告诉你,如果你正在寻找的:

The triangles have a length along the y axis (the side of the column). Divide that number in two and work out how many steps down you are. Based on the number of steps down and if the column is even or odd will tell you if you're looking at:

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

或者相反。在这一点上,你只需要确定该行的一面是,以确定哪些直角三角形它在,这也说明了哪些等距三角它在

or the reverse. At this point you only need to determine which side of the line it's on to determine which right triangle it's in, which also tells you which isometric triangle it's in.

您有几个选项可以实现。

You have a couple of options for this.

  1. 您可以使用类似布氏线算法栅格化斜边,当你击中立柱你在工作,如果你是高于或低于该线;
  2. 因为你只有两种可能的网格在这里(一个是另一个的逆所以真的只有一个)。你可以存储行值的阵列,称为第3列,斜边是在偏移2,而6是在4等。

您甚至可以使用(1)生成(2)作为一个快速查找。

You could even use (1) to generate (2) as a fast lookup.

唯一的其他东西要考虑的是,如果将鼠标光标是在一个边缘会发生什么情况?

The only other thing to consider is what happens if the mouse cursor is on an edge?

这篇关于在三角等距网格,什么三角形是一个给定的点在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆