views:

211

answers:

5

How can i implement “intersection” method using java by recieve 2 integer parameters and return integer intersection point.

+1  A: 

Maybe this could help

Ido
If that's what he means, I'll eat my hat. :-)
Jim Lewis
It was worth a shot ;)
Ido
A: 

If what you want is a function that takes two 'line' objects and returns a coordinate where they intersect, i suggest looking at the formula here

http://en.wikipedia.org/wiki/Line-line_intersection

and doing

func(line a, lineb)
{
  x1 = a.coord1.x;
  y1 = a.coord1.y;
  x2 = a.coord2.x;
  y2 = a.coord2.y;
  //do math and code here
  return line(coord(x1new,y1new),coord(x2new,y2new));
}

If this isn't what you wanted, please refer to Ido's comment :p

pyInTheSky
He said the input was an integer ?!
Ido
hmm, indeed ...I took a leap of mind reading ...since i find it hard to believe that you can have an intersection with only two integers :p
pyInTheSky
maybe it's 'new math' !!
pyInTheSky
A: 

If you want to be really lazy why don't you just use the line2d library. http://download.oracle.com/javase/1.4.2/docs/api/java/awt/geom/Line2D.html It can find the intersect and many other things that have to do with a 2d line already built in.

BT
+1  A: 
grddev
A: 

As far as I can see, if you use two integer parameters, all you really need to do is average the two numbers, and that's the midpoint (intersection, I guess?)

int intersect(int a, int b) {
    return ((a + b) / 2);
}

Otherwise, if you are looking for bitwise intersection, you would used the bitwise-AND operator - &. Here's an example:

int intersect(int a, int b) {
    return (a & b);
}
UrNotSorry