【问题标题】:How does scaling operator (*) work in PostgreSQL?缩放运算符 (*) 在 PostgreSQL 中如何工作?
【发布时间】:2018-08-28 21:19:32
【问题描述】:

我刚开始学习 PostgreSQL,我不知道缩放运算符是如何处理几何类型的。

例如select '((1, 1), (0, 0))'::box * '(2, 0)'::point; 返回((2,2),(0,0))

select '((1, 1), (0, 0))'::box * '(0, 2)'::point; 返回((0,2),(-2,0))

所以在这两种情况下,盒子都会被缩放 2 倍(对于两个轴),但盒子的移动方式对我来说毫无意义。

Official documentation 仅展示了此运算符的一个用法示例,并没有说明它是如何工作的。

如果有人知道更好的 PostgreSQL 学习资源,请分享。

提前致谢。

【问题讨论】:

  • 矩阵 x 向量乘法如何?如果框是 2x2 矩阵并且点是大小为 2(2x1 矩阵)的向量,则将这两者相乘应该得到 2x1 矩阵(不能是框)
  • 文档说它是“缩放/旋转”所以可能(幅度,弧度)
  • 至于“更好的资源”,不妨多试几个例子,就明白了。
  • 例如也许它被视为对角矩阵

标签: sql database postgresql geometry gis


【解决方案1】:

下面是函数box_mul 的实现,它位于*(box, point) 运算符后面(来自https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/geo_ops.c):

static inline void
point_mul_point(Point *result, Point *pt1, Point *pt2)
{
    point_construct(result,
                    float8_mi(float8_mul(pt1->x, pt2->x),
                              float8_mul(pt1->y, pt2->y)),
                    float8_pl(float8_mul(pt1->x, pt2->y),
                              float8_mul(pt1->y, pt2->x)));
}

Datum
box_mul(PG_FUNCTION_ARGS)
{
    BOX        *box = PG_GETARG_BOX_P(0);
    Point      *p = PG_GETARG_POINT_P(1);
    BOX        *result;
    Point       high,
                low;

    result = (BOX *) palloc(sizeof(BOX));

    point_mul_point(&high, &box->high, p);
    point_mul_point(&low, &box->low, p);

    box_construct(result, &high, &low);

    PG_RETURN_BOX_P(result);
}

或翻译成更“人性化”的语言:

((x<sub>1</sub>, y<sub>1</sub>), (x<sub>2</sub>, y<sub>2</sub>)) * (x, y) :- ((x<sub>1</sub>*x - y<sub>1</sub>*y, x<sub>1</sub>*y + y<sub>1</sub>*x), (x<sub>2</sub>*x - y<sub>2</sub>*y, x<sub>2</sub>*y + y<sub>2</sub>*x))

你的例子

((1,1),(0,0)) * (0,2) = ((1*0 - 1*2, 1*2 + 1*0), (0*0 - 0*2, 0*2 + 0 * 0)) = ((-2,2),(0,0))

最后box_construct() 将其转换为(0,2),(-2,0)(只需检查select '((-2,2),(0,0))'::box;

如果您知道/记住这些变换的几何意义 - 请在此处发布您的最终答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    相关资源
    最近更新 更多