计算动态形成的矩形的面积和位置 [英] Calculating the area and position of dynamically formed rectangles

查看:235
本文介绍了计算动态形成的矩形的面积和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello stackoverflow社区,

Hello stackoverflow community,

我正在使用Cocos2D / Box2D玩拼图游戏,玩家在屏幕上绘制线条。根据玩家的绘制情况,我想要绘制作为绘制线结果出现的矩形的区域和位置。

I'm working on a puzzle game using Cocos2D/Box2D were the player draws lines on the screen. Depending on were the player draws, I want to then work out the area and position of the rectangles that appear as a result of the drawn lines.

我目前有在游戏中的所有线的数组,所以我知道他们的(x,y)位置和大小,但我失去了如何计算动态形成的矩形的面积和笛卡尔坐标。为了帮助说明问题,请参阅以下内容:

I've currently got an array of all lines in the game so I know their (x, y) positions and sizes but I'm at a lost as to how to calculate the area and Cartesian coordinates of the rectangles that are dynamically formed. To help illustrate the problem, please see the following:

在上图中,您可以看到一个黑色边框。其中包含由玩家绘制的4条灰线。由此,形成了5个蓝色矩形。任何有关如何计算矩形的面积和笛卡尔坐标的指导或建议将是一个很大的帮助。

In the image above, you can see a black border. Contained within this are 4 grey lines which have been drawn by the player. From this, 5 blue rectangles have been formed. Any guidance or advice on how I can calculate the area and Cartesian coordinates of the rectangles would be a great help.

推荐答案

如果将行更容易转换成一组矩形?

I wonder if it would be easier to convert the lines into a set of rectangles?

从只包含主要大矩形的矩形列表开始。对于每一行,看看列表中的哪个矩形包含它。

Start with a list of rectangles which only contains the main big rectangle. For each line, see which rectangle in the list contains it. Remove that rectangle from the list of rectangles and replace it with 2 smaller rectangles defined by the line.

一旦你有矩形列表,你可以很容易地计算它们的面积by只是(width * height)

Once you have the list of rectangles, you can easily calculate their area by just doing (width * height).

这篇关于计算动态形成的矩形的面积和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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