在 Objective-C 中创建向量类

发布于 2024-12-29 00:19:43 字数 368 浏览 3 评论 0原文

我决定使用严格的 Objective-C 为我的应用程序创建一个 Vector 类。我认为发生这种情况的方法是创建一个 Point2D 类,首先以 (x,y) 格式保存每个点,然后创建一个保存 2 个点的 Vector2D 类。

整个事情陷入困境的是我的 Vector2D 类。我试图找到一种方法来在我的 Vector2D 类的实例变量中保存 2 个对象(实际上是 2 个 Point2D 对象)。 我想到了 NSMutableArray 但我记得在这种情况下有很多问题,而且我不确定这是否是最有效的解决方案,因为我正在处理浮点数。

我想要的是通过 Objective-c 的 Vector 类提供某种指导,或者您对我的“任务”的提示/建议。 你会选择如何做这样的事情以及需要额外注意什么。

I decided to create a Vector Class for my app in strict objective-C. The way i thought this could happen is to create a Point2D class first holding each point in a (x,y) format and then create a Vector2D class holding 2 points.

Where the whole thing got stuck is in my Vector2D class. Im trying to find a way to hold 2 objects (actually 2 x Point2D objects) in an instance variable in my Vector2D class.
I thought of an NSMutableArray but i recall many problems in this situation, plus im not sure is the most efficient solution since im dealing with floats.

What i would like is some sort of guidance through a Vector class for objective-c or your tips/recommendations on my "quest".
How would you choose to do such a thing and also what would require extra attention.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2025-01-05 00:19:43

固定大小的 C 数组(在您的情况下为 2)在您的情况下应该是完全可以接受的。由于你的向量不会动态增长,我认为这应该可以正常工作:

@interface Vector2D : NSObject {
    Point2D *points[2];
}
// Properties and methods
@end

C array of fixed size (2 in your case) should be perfectly acceptable in your situation. Since your vector does not grow dynamically, I think this should work just fine:

@interface Vector2D : NSObject {
    Point2D *points[2];
}
// Properties and methods
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文