Postgresql 几何线 - 找到 Y 截距

标签 postgresql geometry

我有一个可以用多个线性段表示的模型,如下所示:

 Y
  |   _/_________\_
  |   /           \
  |  /             \
  | /               \
  |/                 \
  |           
  |________________________ X

我需要找到给定 X 值的 Y 值

虽然我最初是将每个段存储为关系类型{A, B, C}。但是,我不确定在找到合适的查询来检索 Y 值方面,这对我有什么帮助。

最佳答案

由于您使用的是线性段,因此您应该使用 lseg 数据类型(line 数据类型代表一条无限长的线)。获得该格式的数据后,您可以在所需的 X 值处找到线段与无限长垂直线的交点,并提取交点的 Y 值。

CREATE TABLE segments (id int, seg lseg);

INSERT INTO segments VALUES 
  (1, '[(4,3), (12,15)]'),  -- positively inclined line segment
  (2, '[(2,19), (24,-4)]'), -- negatively inclined line segment
  (3, '[(4,3), (12,3)]'),   -- horizontal line segment
  (4, '[(5,3), (5,15)]'),   -- vertical line segment, collinear at X=5
  (5, '[(4,3), (4,15)]');   -- vertical line segment, no intersection at X=5

然后:

test=# SELECT id, 5 AS x, (seg # '((5,-999999999), (5,999999999))'::lseg)[1] AS y
test-# FROM segments;
 id | x |        y
----+---+------------------
  1 | 5 |              4.5
  2 | 5 | 15.8636363636364
  3 | 5 |                3
  4 | 5 |
  5 | 5 |
(5 rows)

从上面可以明显看出,共线线段(即 X 具有相同值的垂直线段)和没有交集的线段对于 Y 返回 NULL

关于Postgresql 几何线 - 找到 Y 截距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31263815/

相关文章:

Python/postgres/psycopg2 : getting ID of row just inserted

postgresql - 使用多表子句将多个行值选择为单行

sql - 错误 : more than one row returned by a subquery used as an expression

ruby-on-rails - 基于Ruby中的第二个数组对多维数组进行排序

javascript - 使用 SVG 弧线绘制部分圆

java - 2 个 Java GeneralPath 之间的带状线

math - 平面的 3D 旋转

css - 使用 CSS 设置 SVG 圆圈样式

java - 如何在没有 SELECT 的情况下调用 postgres 中的函数?

java - 数学问题 : place circles around the clock