在数据库中存储形状的最佳方式是什么

发布于 2024-11-26 01:40:38 字数 1432 浏览 2 评论 0原文

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

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

发布评论

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

评论(5

情深已缘浅 2024-12-03 01:40:38

从技术上讲这不是一个答案,但也许这里的问题是 SQL?我认为像 CouchDB 这样的文档存储系统可能是这种情况下更有效的解决方案。

我在想这样的事情:

{
    "_id": "whatever",
    "_rev": "whatever",
    "boundingBox": [
        [0 0],
        [2 2]
    ],
    "size": [2 2],
    "circle": {
        "center": [1 1],
        "radius": 1
    }

}

"circle" 节将根据形状更改名称和详细信息。矩形有角(类似于 "boundingBox"),椭圆体有......无论什么定义椭圆体:p

Technically not an answer, but maybe the problem here is SQL? I'm thinking a document-storage system like CouchDB might be a much more effective solution for this scenario.

I'm thinking something like this:

{
    "_id": "whatever",
    "_rev": "whatever",
    "boundingBox": [
        [0 0],
        [2 2]
    ],
    "size": [2 2],
    "circle": {
        "center": [1 1],
        "radius": 1
    }

}

The "circle" stanza would change name and details depending on the shape. Rectangles would have the corners (similar to "boundingBox"), ellipsoids would have... Whatever defines ellipsoids :p

破晓 2024-12-03 01:40:38

一些简单的想法:

  • 您可以存储定义多边形的顶点列表。
  • 您可以存储定义多边形边缘的向量列表。

它们如何存储在数据库中:

  • 形状主表,主键为称为“shapeID”的标识
  • 向量表,主键标识向量ID,外键为适当的ShapeID,原点(x,y,z坐标) 、方向 (x,y,z) 和长度。

可以将其他元数据分配给具有其他表的形状,这些表也通过外键引用 shapeID。

Some simple ideas:

  • You could store a list of vertices that define the polygon.
  • You could store a list of vectors that define the edges of the polygon.

How they could be stored in a database:

  • Master table of shape, with primary key an identity called "shapeID"
  • Vector table, with a primary key identifying the vectorID, foreign key to the appropriate ShapeID, origin (x,y,z coords), direction (x,y,z), and length.

Additional meta data can be assigned to a shape with other tables that also reference the shapeID by foreign key.

轻拂→两袖风尘 2024-12-03 01:40:38

确实没有一种“最佳”方法可以在数据库中存储继承层次结构。大多数数据库课程教授 3 种模型:带空值的单表、每个子类表和每个具体类表。您可以在 这篇博文。如果您想简单地映射数据库中形状的属性,则可以选择您喜欢的其中之一。

如果你想要更高级的东西,MySql 提供了 Geometry 类型 专门用于存储几何形状。

There isn't really a "best" way to store an inheritance hierarchy in a database. There are 3 models that are taught in most database courses: Single Table with nulls, Table per SubClass, and Table per Concrete Class. You can find a good explanation in this blog post. You can choose whichever one of these that you prefer if you want to simply map the attributes of your shapes in the database.

If you want something more advanced to, MySql provides a Geometry type that is specifically designed to store geometric shapes.

猫卆 2024-12-03 01:40:38

我投票:

  • 制作一个包含所有可能字段的形状表:

    • 例如宽度和高度
    • 底面和高度
    • 半径
      ...
    • 形状类型(例如 0= 圆形、1= 方形、3= 三角形...)
  • 一张表,所有可能的形状,一个形状实例/行

我直言...

问:您认为您可能有多少种不同的形状?你认为每个形状可能有多少列?

附:
“多边形”(由任意数量的点组成)可能需要两个单独的表。但大多数形状(圆形、椭圆形、正方形、矩形等)每个实例不需要超过 4 或 5 列,并且应该可以轻松放入一张表中。

I'd vote:

  • Make a Shape table with all possible fields:

    • like the width and height
    • base and height
    • radius
      ...
    • shape type (e.g. 0= circle, 1= square, 3= triangle, ...)
  • One table, all possible shapes, one shape instance/row

IMHO...

Q: How many different shapes do you think you might have? How many columns do you think each shape might take?

PS:
A "polygon" (consisting of an arbitrary number of points) might merit two separate tables. But most shapes (circle, ellipse, square, rectangle, etc) shouldn't need more than 4 or 5 columns/instance, and should easily fit in one table.

愿得七秒忆 2024-12-03 01:40:38

我选择了第二种解决方案,因为它易于管理和扩展。序列化选项不实用,因为如果序列化模型的结构发生变化(例如:添加属性),我必须管理模型版本。第三种选择并不是构建数据库的正确方法。

I opted for the second solution because is easy to manage and extend. The serialization option is not practical because if the structure of the serialized model changes (example : adding an attribute), I have to manage the model versions. The third option is just not the right approach of building databases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文