Geometric bewilderment
#!/usr/bin/perl
use DBI;
use warnings;
use strict;
my $dbname = shift or die;
my ($dbh, $stm);
$dbh = DBI->connect("dbi:Pg:dbname=$dbname", "", "") or die $!;
$dbh->do(<<EOSQL) or die $!;
DROP TABLE IF EXISTS points;
CREATE TABLE points(
pointpoint point,
pointbox box
);
EOSQL
for( my $i=0; $i<1000; $i++ ) {
my $x = rand(2.0)-1.0;
my $y = rand(2.0)-1.0;
$dbh->do(<<EOSQL) or die $!;
INSERT INTO points VALUES (
Point('$x,$y'),
Box(Point('$x,$y'),Point('$x,$y'))
);
EOSQL
}
check_box_point('<<');
check_box_point('&<');
check_box_point('&&');
check_box_point('&>');
check_box_point('>>');
check_box_point('@>');
check_box_point('&<|');
check_box_point('<<|');
check_box_point('|>>');
check_box_point('|&>');
check_point_box('<<');
check_point_box('&<');
check_point_box('&&');
check_point_box('&>');
check_point_box('>>');
check_point_box('<@');
check_point_box('&<|');
check_point_box('<<|');
check_point_box('|>>');
check_point_box('|&>');
#--------------------------------------------------------------------------
# Check 'box OP point' against 'box OP box(point,point)'
#--------------------------------------------------------------------------
sub check_box_point {
my $op = shift;
print "box $op point\n";
$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('-0.5,-0.5'),point('0.5,0.5')) AS b
WHERE
b $op pointbox
ORDER BY
x, y;
EOSQL
$stm->execute() or die $!;
open FH, ">a.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;
$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('-0.5,-0.5'),point('0.5,0.5')) AS b
WHERE
b $op pointpoint
ORDER BY
x, y;
EOSQL
$stm->execute() or die $!;
open FH, ">b.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;
system( "tkdiff a.out b.out >/dev/null 2>&1" );
}
#--------------------------------------------------------------------------
# Check 'point OP box' against 'box(point,point) OP box'
#--------------------------------------------------------------------------
sub check_point_box {
my $op = shift;
print "point $op box\n";
$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('0.25,0.25'),point('0.75,0.75')) AS b
WHERE
pointbox $op b
ORDER BY
x, y;
EOSQL
$stm->execute() or die $!;
open FH, ">a.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;
$stm = $dbh->prepare(<<EOSQL) or die $!;
SELECT
pointpoint[0] as x,
pointpoint[1] as y
FROM
points AS p,
box(point('0.25,0.25'),point('0.75,0.75')) AS b
WHERE
pointpoint $op b
ORDER BY
x, y;
EOSQL
$stm->execute() or die $!;
open FH, ">b.out";
while( my $ref = $stm->fetchrow_hashref() ) {
my $x = $ref->{x};
my $y = $ref->{y};
print FH "$x,$y\n";
}
close FH;
system( "tkdiff a.out b.out >/dev/null 2>&1" );
}
On Wed, Aug 19, 2009 at 07:29:43PM +1000, Paul Matthews wrote:
Suspect that attaching large amounts of code is a breach of
etiquette.
Code attachments aren't, but HTML messages are, so in future, please
send only text :)
Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate