Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bEjYB-0004qW-3e for pgsql-bugs@arkaria.postgresql.org; Sun, 19 Jun 2016 20:42:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bEjYA-0003dh-F2 for pgsql-bugs@arkaria.postgresql.org; Sun, 19 Jun 2016 20:42:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bEjY9-0003dL-Lh for pgsql-bugs@postgresql.org; Sun, 19 Jun 2016 20:42:33 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bEjY6-0007ui-2T for pgsql-bugs@postgresql.org; Sun, 19 Jun 2016 20:42:33 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u5JKgOqR019817; Sun, 19 Jun 2016 16:42:24 -0400 From: Tom Lane To: emre@hasegeli.com cc: PostgreSQL Bugs Subject: Re: Line intersection point is wrong In-reply-to: References: <14750.1466353137@sss.pgh.pa.us> <9953.1466360251@sss.pgh.pa.us> <18081.1466367402@sss.pgh.pa.us> Comments: In-reply-to Emre Hasegeli message dated "Sun, 19 Jun 2016 22:30:08 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <19712.1466368760.0@sss.pgh.pa.us> Date: Sun, 19 Jun 2016 16:42:24 -0400 Message-ID: <19816.1466368944@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <19712.1466368760.1@sss.pgh.pa.us> Emre Hasegeli writes: >> You haven't done anything to exclude the possibility that l1->B is zero, >> so you could be getting zero-divide in the y calculation. > Ah, yes. We can use l2 when l1->B is zero. After working out the algebra by hand, I think the attached is correct (and it does pass the regression tests, yay). I also made the line_parallel and line_perp tests more symmetric and zero-divide-free. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-parallel-perp-and-intersect.patch"; charset="us-ascii" Content-ID: <19712.1466368760.2@sss.pgh.pa.us> Content-Description: fix-parallel-perp-and-intersect.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_op= s.c index 657bcee..ce796c7 100644 *** a/src/backend/utils/adt/geo_ops.c --- b/src/backend/utils/adt/geo_ops.c *************** line_parallel(PG_FUNCTION_ARGS) *** 1108,1117 **** LINE *l1 =3D PG_GETARG_LINE_P(0); LINE *l2 =3D PG_GETARG_LINE_P(1); =20=20 ! if (FPzero(l1->B)) PG_RETURN_BOOL(FPzero(l2->B)); =20=20 ! PG_RETURN_BOOL(FPeq(l2->A, l1->A * (l2->B / l1->B))); } =20=20 Datum --- 1108,1119 ---- LINE *l1 =3D PG_GETARG_LINE_P(0); LINE *l2 =3D PG_GETARG_LINE_P(1); =20=20 ! if (FPzero(l1->A)) /* horizontal? */ ! PG_RETURN_BOOL(FPzero(l2->A)); ! if (FPzero(l1->B)) /* vertical? */ PG_RETURN_BOOL(FPzero(l2->B)); =20=20 ! PG_RETURN_BOOL(FPeq(l2->A * l1->B, l1->A * l2->B)); } =20=20 Datum *************** line_perp(PG_FUNCTION_ARGS) *** 1120,1131 **** LINE *l1 =3D PG_GETARG_LINE_P(0); LINE *l2 =3D PG_GETARG_LINE_P(1); =20=20 ! if (FPzero(l1->A)) PG_RETURN_BOOL(FPzero(l2->B)); ! else if (FPzero(l1->B)) PG_RETURN_BOOL(FPzero(l2->A)); =20=20 ! PG_RETURN_BOOL(FPeq(((l1->A * l2->B) / (l1->B * l2->A)), -1.0)); } =20=20 Datum --- 1122,1133 ---- LINE *l1 =3D PG_GETARG_LINE_P(0); LINE *l2 =3D PG_GETARG_LINE_P(1); =20=20 ! if (FPzero(l1->A)) /* horizontal? */ PG_RETURN_BOOL(FPzero(l2->B)); ! if (FPzero(l1->B)) /* vertical? */ PG_RETURN_BOOL(FPzero(l2->A)); =20=20 ! PG_RETURN_BOOL(FPeq(l2->A * l1->B, -(l1->A * l2->B))); } =20=20 Datum *************** line_interpt_internal(LINE *l1, LINE *l2 *** 1233,1250 **** =20=20 if (FPzero(l1->B)) /* l1 vertical? */ { ! x =3D l1->C; ! y =3D (l2->A * x + l2->C); } else if (FPzero(l2->B)) /* l2 vertical? */ { ! x =3D l2->C; ! y =3D (l1->A * x + l1->C); } else { ! x =3D (l1->C - l2->C) / (l2->A - l1->A); ! y =3D (l1->A * x + l1->C); } result =3D point_construct(x, y); =20=20 --- 1235,1252 ---- =20=20 if (FPzero(l1->B)) /* l1 vertical? */ { ! x =3D -(l1->C / l1->A); ! y =3D -(l2->A * x + l2->C) / l2->B; } else if (FPzero(l2->B)) /* l2 vertical? */ { ! x =3D -(l2->C / l2->A); ! y =3D -(l1->A * x + l1->C) / l1->B; } else { ! x =3D (l1->B * l2->C - l2->B * l1->C) / (l1->A * l2->B - l2->A * l1->B); ! y =3D -(l1->A * x + l1->C) / l1->B; } result =3D point_construct(x, y); =20=20 ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--