Intersection of Two Lines

7

Given equation of two lines in the format Ax+By=C, determine their intersection point. If they are parallel print 'parallel' without quotes. For input/output format refer sample input/output.

Input
1x+1y=1
1x-1y=1

Output
(1.00,0.00)

Input
0x+1y=0
1x+0y=0

Output
(0.00,0.00)
  1. -1000000 <= A,B,C <= 1000000.
  2. Note their intersection point may not be integer. Print upto two decimal places.
  3. Shortest solution wins.

fR0DDY

Posted 2011-04-15T09:55:18.627

Reputation: 4 337

Is a function call valid? example : f[1x+1y=1, 1x-1y=1] – Dr. belisarius – 2011-04-17T20:58:33.117

@belisarius Yes, but the solution should be entire program. – fR0DDY – 2011-04-18T05:59:37.737

Is the format always Ax+By=C or can it also be Ax-By=C or even -Ax+By=C? Are negative numbers for A, B or C even allowed? »Print up to two decimal places« implies that less can be printed, your examples do not suggest that. If the output is locale-dependent (which can happen with plenty languages in formatted output), can we assume en-US or C as locale? (I.e. anything that uses a decimal point and not a comma). – Joey – 2011-04-18T10:43:54.030

@Joey Yes A,B and C can be negative. Ax-By=C is already given in one of the examples. The points should have 2 decimal places. – fR0DDY – 2011-04-18T11:45:01.850

@Joey Edited the question to show that. Thanks for pointing that out. – fR0DDY – 2011-04-18T13:49:01.947

Answers

2

Ruby - 119 chars

a,b,c,d,e,f=(gets+gets).scan(/-?\d+/).map &:to_f
puts (t=a*e-b*d)==0?'parallel':"(%.2f,%.2f)"%[(e*c-b*f)/t,(a*f-c*d)/t]

Wile E. Coyote

Posted 2011-04-15T09:55:18.627

Reputation: 943

It's odd but my ruby 1.9.x shows -0.00, :-\ and you can use gets(p) instead of 2 gets, it will read the entire contents (assuming it was piped) – st0le – 2011-04-16T15:55:45.193

1gets(2) would get two bytes of input, as opposed to two lines on input. – britishtea – 2014-09-29T12:23:41.477

2

Python, 148 146 chars

import re
I=raw_input
a,b,p,c,d,q=eval(re.sub('x|y=','.,',I()+','+I()))
D=a*d-b*c
print'(%.2f,%.2f)'%((p*d-q*b)/D,(a*p-c*q)/D)if D else'parallel'

Keith Randall

Posted 2011-04-15T09:55:18.627

Reputation: 19 865

1

Python+sympy - 161 chars

from sympy import*
x,y=symbols('xy')
e=lambda:eval(raw_input().replace('x','*x').replace('y=','*y-'))
r=solve([e(),e()],[x,y])
print r and`r[x],r[y]`or'parallel'

Python+sympy - 143 chars (different output format)

from sympy import*
x,y=symbols('xy')
e=lambda:eval(raw_input().replace('x','*x').replace('y=','*y-'))
print solve([e(),e()],[x,y])or'parallel'

output format for 143 char version is slightly different from the spec

In:
1x+1y=1
1x-1y=1

Out:
{x: 1, y: 0}

In:
0x+1y=0
1x+0y=0

Out:
{x: 0, y: 0}

gnibbler

Posted 2011-04-15T09:55:18.627

Reputation: 14 170

1

sage - 162 chars

x,y=var('x,y')
e=lambda:eval(raw_input().replace('x','*x').replace('y','*y='))
try:r=solve([e(),e()],[x,y])[0];print`r[0].rhs(),r[1].rhs()`
except:print'parallel'

gnibbler

Posted 2011-04-15T09:55:18.627

Reputation: 14 170

1

J, 146 132 134 124

echo'parallel'"_`(1|.')(',[:(,',',])&(0j2&":)/[:,,.@{:%.}:)@.([:*@|[:-/ .*}:)|:".>{.`(<@}:@;@(1 2&{))`{:(`:0)"1;:;._2(1!:1)3

Parsing this is awful.

Edit: Realized that my output is basically broken, although it works for the examples given and parallels...

Edit: Posting the shorter version I already had.

Edit: Fixed without too much pain...

Edit: Fixed a weird issue where the program was spread across multiple lines.

Somewhat ungolfed version:

words =: ;:;._2(1!:1)3
NB. Leverage J's parser itself to split each line into e.g. ax + by = c
lines =: ".>{.`(<@}:@;@(1 2&{))`{:(`:0)"1 words
NB. Use a 3-part gerund (`) to get boxed strings 'ax';'b';'c', unbox and parse
calc_and_format =: 1|.')(',[:(,',',])&(0j2&":)/[:,,.@{:%.}:
NB. %. (matrix multiply) first two rows (a d/b e) by third row turned (c/f)
NB. And then format laboriously. 0j2&": formats numbers with 2 decimals.
det_nonzero =: [:*@|[:-/ .*}:
NB. 1 if determinant (-/ .*) of (a b/d e) is nonzero (*@|)
echo'parallel'"_`calc_and_format@.det_nonzero|: lines
NB. transpose parsed input to get (a d/b e/c f). 
NB. If det_nonzero, call calc_and_format, otherwise constant 'parallel'
NB. Print

Jesse Millikan

Posted 2011-04-15T09:55:18.627

Reputation: 1 438

1

OCaml + Batteries, 163 characters

As straightforward as it gets:

Scanf.scanf"%fx%fy=%f\n%fx%fy=%f\n"Float.(fun a b c d e
f->Printf.(fun u->if u=0.then printf"parallel"else
printf"(%.2f,%.2f)"((b*f-c*e)/u)((d*c-a*f)/u))(b*d-a*e))

Edit:

  • Initial version, 169
  • Use Batteries for delimited overloading of operators, 164
  • Lambda binding of u, 163

Matías Giovannini

Posted 2011-04-15T09:55:18.627

Reputation: 281

1

C-149 bytes

Not much of golfing just the basics.

float a,b,c,d,m,n,t;main(){scanf("%fx%fy=%f%fx%fy=%f",&a,&b,&m,&c,&d,&n);t=b*c-a*d;t?printf("(%.2f,%.2f)",(b*n-d*m)/t,(c*m-a*n)/t):puts("parallel");}

Here is the ideone link for testing.

Instead of printing 'parallel' an alternative could be to print the orthogonal distance between the lines when they are parallel.

Quixotic

Posted 2011-04-15T09:55:18.627

Reputation: 2 199