指向多边形 [英] Point in polygon

查看:87
本文介绍了指向多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个点是否在多边形内。我正在使用vector std c ++。


我的程序到现在为止:

I am trying to find out whether a point is inside a polygon.I am using vector std c++.

My program till now:

展开 | 选择 | < span class =codeLinkonclick =WordWrap(this);> Wrap | 行号

推荐答案

此处,polygon也是一般词。将它用作基类。然后派生你的多边形:正方形,三角形,矩形,五角形等。尽量不要开始解决一个类中的所有多边形。


然后在每个派生类中编写一个转换方法构建数据库。每个派生类可以具有不同的算法和数据库。基类中的所有内容都是你在派生类中重写的函数原型。


你的方块只是矩形所以你可能从矩形中得到正方形以允许方形到使用与矩形相同的数据库。也许有一个衍生自多边形的平行图类,然后从中得到正方形和矩形。


这个想法是你一次实现一个形状。弄清楚一个点是在矩形还是三角形的内部或外部是非常简单的,所以首先解决这些问题。你可以计算这些。不需要数据库。


之后如果你的多边形是一个矩形,其中有一个三角形,你可以使用已经写好的矩形和三角形代码找出你的观点在我们外面的形状。就像它在矩形内部并且也在三角形内部一样,它就在形状之外。在你的类中是成员变量,它们是Rectangle和Triangle对象。


C ++希望你写一点并让它工作。然后写一点,让它工作。通过反复的改进周期,您可以拥有这种强大的功能,并且在程序运行的整个过程中。任何错误都可能只是在最后一个改进周期。
Here again, polygon is a general word. Use it as a base class. Then derive your polygons: Square, Triangle, Rectangle, Pentagon, etc. Try to not start out to solve all polygons in one class.

Then in each derived class write a convert method to build the database. Each derived class can have a different algorithm and database. All that''s in the base class is the function prototype that you override in the derived class.

Your square is just rectangle so maybe you derive square from rectangle to allow the square to use the same database as the rectangle. Maybe there is a parallogram class deriving from polygon and then square and rectangle derive from that.

The idea is that you implement a shape at a time. Figuring out if a point is inside or outside a rectangle or triangle is pretty straightforward so solve those first. You can just calculate these. No database required.

later when you have a polygon that''s a rectangle with a triangle punched out of it you can use the already written rectangle and triangle code to find out of your point is inside our outside the shape. Like if it is inside the rectangle and also inside the triangle, then it is outside the shape. In your class are member variables that are Rectangle and Triangle objects.

C++ expects you to write a little and get that working. Then write a little more and get that working. By repeated improvement cycles you have this humongous capability and all along the way the program has been working. Any errors are probably in just the last improvement cycle.


感谢您的建议。我照你说的做了。首先,我创建了一个名为Shape的基类和一个名为Polygon的派生类。在基类中,我使用函数calculateDistance来计算2点之间的距离。

Shape.cc
Thanks for your suggestion. I did as you told. First I created a base class called Shape and a derived class called Polygon. In the base class, I used a function calculateDistance to calculate distance between 2 points.
Shape.cc
展开 | 选择 | Wrap | 行号


您发布的代码中没有错误。 />

发布编译器给你的确切错误。
There is no error in the code you posted.

Post the exact error your compiler gave you.


这篇关于指向多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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