如何从地图中选择国家/地区? [英] How can I select a country from a map?

查看:306
本文介绍了如何从地图中选择国家/地区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

温和的社区成员,


我想显示带有图像的表格,欧洲地图。

如何实现事件处理程序显示属于所选国家/地区的信息。


我已经放弃使用复杂的多边形和椭圆形状。


我发现了另一个办法。我下载了一个国家边界的地图。 我用不同的颜色填充每个国家。如果用户点击一个点,那么事件处理程序会读取它的颜色。我将在我的程序中包含一个表格,以便选择具有所选颜色的国家/地区并显示与该国家/地区相关的信息。

Gentle Community Members,

I want to display a form with an image, the map of Europe.
How can I achieve that the event handler displays the information belonging to the selected country.

I have discarded the usage of complicated polygon and elliptical shapes.

I have found out another way. I download a map with borders of countries. I fill each country with unique different color. If the user clicks onto a point then the event handler reads the color of it. I will include a table in my program in order to select the country having the chosen color and display the information connected to this country.

展开 | 选择 | 换行 | 行号

推荐答案

我个人主张使用多边形只是因为用于检测点是否在多边形内部的算法实际上并没有那么糟糕,并且因为这种方式你只限于一组的颜色。话虽如此,RGB空间有很多颜色,所以你可能还可以。


如果您想知道要使用哪个事件,您应该查看MouseMove事件。我相信事件args将为您提供一个位置,然后您可以使用该位置映射回您的图像。 Bitmap对象具有GetPixel方法,该方法将返回该坐标处的Color。如果你正在绘制任何缩放/过滤,你需要考虑到这一点...所以也许现在让它使用一个未缩放的位图。
Personally I would advocate the usage of polygons only because the algorithm for detecting if a point is inside a polygon is actually not that bad, and because with this way you''re limited to a set of colours. Having said that, RGB space has a ton of colours so you''re probably ok.

If you''re wondering which event to use, you should probably look into the MouseMove event. I believe the event args will give you a position which you can then use to map back to your image. A Bitmap object has a GetPixel method, which will return the Color at that coordinate. If you''re drawing with any scaling/filtering, you''ll need to take that into account... so maybe get it working with an unscaled bitmap for now.


谢谢对于这些建议,Gary。
在哪里可以找到检测点是否在多边形内的算法?


现在我有一个欧洲国家的数据库和相应的地图。
Thank you for the suggestions, Gary.
Where can I find the algorithm for detecting whether a point is inside a polygon?

Now I have a database of European Countries and the corresponding map.
展开 | 选择 | Wrap | 行号


检测点是否在多边形内的算法是选择几何体外的点并创建该点与点击点之间的一条线。如果该测试线与您的多边形相交奇数次,则该点位于多边形内部。也就是说,测试多边形的每个线段,每次测试线相交时,递增一个计数器。如果您的多边形很复杂,您可能希望实现某种QuadTree或其他空间分区算法,这样您就可以更快速地从单击到多边形,而无需测试每一个多边形。我在此处的见解部分中发布了一个基本的QuadTree算法: http: //bytes.com/topic/c-sharp/insig...implementation


您可以通过边界框包含多边形并将其插入四边形树。使用鼠标的单击位置为您提供单击的壁橱多边形,然后您可以测试这些多边形的交叉点。你提到你的多边形是复杂的,但我怀疑如果你使用四叉树你会看到1000多个交叉测试,今天的处理器应该处理没问题。


正如我之前提到的,这只是你想要真正准确的检测。你的颜色方法应该有效,你可能会遇到一些带边框检测的麻烦(因为我注意到你将所有的边框涂成黄色),如果你需要添加更多的国家,或者你决定要改变颜色(虽然你可以使用命中检测地图和绘制地图。


所以关于你如何比较颜色的第二个问题,你真的不需要那么复杂。正如我所提到的,Bitmap.GetPixel(x,y)方法将返回一个Color对象。您可以使用它直接与您拥有的颜色进行比较。


这是一个假设您有一个名为clickLocation的Point,一个Bitmap mapImage和一个Country对象列表的示例有一个MapColor属性(你已经从数据库或文件加载)...

The algorithm for detecting if a point is inside a polygon is a matter of picking a point outside the geometry and creating a line between that point and the click point. A point is inside the polygon if that test line intersects with your polygon an odd number of times. That is, test each line segment of your polygon and every time that test line intersects, increment a counter. If your polygons are complex, you might want to implement some sort of QuadTree or other space partitioning algorithm so you can more quickly get from your click to your polygon without having to test every single one. I have a basic QuadTree algorithm posted in the insights section here: http://bytes.com/topic/c-sharp/insig...implementation

You could contain your polygons by their bounding box and insert them as such into the quad tree. Use your mouse''s click location to give you the closet polygons to the click and then you can test those for intersection. You mentioned your polygons are complex, but I doubt you''d be looking at more than 1000 intersection tests if you use the quad tree and today''s processors should handle that no problem.

As I mentioned before though, this is only if you want really accurate detection. Your colour method should work, you just might run into a few troubles with border detection (as I noticed you coloured all the borders in yellow), if you need to add more countries, or if you decide you want to change the colours (though you can use a hit detection map and a draw map).

So with regards to your second question of how to compare the colours, you don''t really need to get that complicated. As I mentioned, the Bitmap.GetPixel(x, y) method will return a Color object. You can use this to directly compare against the colours you have.

Here''s an example assuming you had a Point called clickLocation, a Bitmap mapImage, and a list of Country obects that have a MapColor property (that you''d have loaded from the database or file)...

展开 | 选择 | 换行 | 行号


这篇关于如何从地图中选择国家/地区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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