aspose file tools*
The moose likes Beginning Java and the fly likes finding the intersection of two lines-  for a junit test case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finding the intersection of two lines-  for a junit test case" Watch "finding the intersection of two lines-  for a junit test case" New topic
Author

finding the intersection of two lines- for a junit test case

fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

can anyone help me to figure out what is wrong with this code? I am trying to write a code that will find the intersection of two lines something I can use in a j-unit test case. here is what i have so far. I am using netbeans.



public class intercept
float line;
{


public intercept(float m1, float m2, float c1, float c2)

Array[] line = new Array;


array[0] = [m2 - m1]/ [c2-c1]
array[1] = [m2-m1]/[c2-c1]
if(float i=0; i=line; i++ ){
line = i;
}
system.out.println(line)
else
system.out.println(" the line don't meet");



}

greatly appreciate help!
Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
First thing array[0] and array[1] are the same thing. Next, what does m1, m2 and c1, c2 mean?



regards,
Victor M. Pereira
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

this is suppose to be the line. i i thought since i declared the as parameters then i could say they were some number. i was going for the point slope formula.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi Tito, and welcome to the Ranch!

When you post a question, you need to TellTheDetails(⇐click) about the problems you're having. We're not mind-readers, after all.

Also, when you post code, please UseCodeTags(⇐click) so it will be readable.

As for this:


I think you need to go back and review loops and if statements. That's just a hot mess.

Finally, a couple of notes about floating point arithmetic. There's really no good reason to use float. Always use double. And when looking for equality, due to natural errors inherent in floating point numbers, you can't check for exact equality. You need to see if the value is with in +/- some error tolerance that you decide on.

Although, for calculating the intercept, you don't actually need to check for equality anyway, but you will want to test your results and see if they're what you expected.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

tito ortizz wrote:this is suppose to be the line. i i thought since i declared the as parameters then i could say they were some number. i was going for the point slope formula.


But look at these two lines:


They're setting both array elements to the exact same value. Why are you doing that?
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

i was thinking the same thing, but i needed to only use 4 variable so i thought i would use the array to run them twice then the result would give me the intersection. how can i fix this problem? and the if and looping are wrong? why? I have just got on here and dont know my way around. sorry for not post all the information.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

tito ortizz wrote:i was thinking the same thing, but i needed to only use 4 variable so i thought i would use the array to run them twice then the result would give me the intersection.


I have no idea what you're saying here.

how can i fix this problem?


I don't know what that code is supposed to actually represent, so I can't answer that.

and the if and looping are wrong? why?


The code you provided won't even compile. That code I pointed out is neither a valid loop nor a valid if statement. Like I said, you need to study those concepts. Once you do, you'll be able to produce a correct loop or a correct if statement, whichever one you need in a particular situation.
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14




Write a program block to compute interaction point between given two lines. Each line is represented in a parametric form –

Y = mX + c

Where m is slop of the line while c is y intercept. In turn your function takes four parameters, m1, c1, m2, and c2 as floating point numbers. The program computes the intersection point and returns its result to the caller. Once completed, perform unit testing of your routine using the method covered in the lab. You must select input values for the unit test using following methods –

·Equivalent partitioning

·Basic path coverage

Provide your analysis for selecting input values in a separate document.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

tito ortizz wrote:


Write a program block to compute interaction point between given two lines. Each line is represented in a parametric form –

Y = mX + c

Where m is slop of the line while c is y intercept. In turn your function takes four parameters, m1, c1, m2, and c2 as floating point numbers. The program computes the intersection point and returns its result to the caller. Once completed, perform unit testing of your routine using the method covered in the lab. You must select input values for the unit test using following methods –

·Equivalent partitioning

·Basic path coverage

Provide your analysis for selecting input values in a separate document.


Okay, so what is your specific question at this point?

And have you figured out how to find the intersection without Java? That has to be your first step.
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

yea, i know that you have to set each point slope form to each other:
0 =(m1 - m2)x + (c1 - c2)


does this look like i am on the right track?

[code=jva]public class intercept

float i;
{


public intercept(float m1, float m2, float c1, float c2)

Array[] line = new Array;


array[0] =(m1 - m2)x + (c1 - c2)
array[1] =(m1 - m2)y + (c1 - c2)
if(float i=0; i<=line.length; i++ ){
this.line =i;
}
system.out.println(line)
else
system.out.println(" the line don't meet");



}


]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I suggest you try to compile it.

Then, when you get a bazillion errors, gut it and start from scratch, adding a tiny bit at a time. Make sure each piece compiles and executes properly before moving on to the next. (For instance, you don't appear to have studied ifs and loops yet.)
Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
fred perez wrote:yea, i know that you have to set each point slope form to each other:
0 =(m1 - m2)x + (c1 - c2)


array[0] =(m1 - m2)x + (c1 - c2)
array[1] =(m1 - m2)y + (c1 - c2)



Before you write the code, make sure you got your math right.

The basic of a line is:
y1 = m1x + c1

And to find the intersection of two equations you have to make y1 = y2.


fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

is this code any better?


Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
In that code your assigning to y2 the value of y1.

try making java calculate the point of interception according to your math.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred perez wrote:
{ y1 = m1 + c1;
y2 = m2 + c2;
y2 = y1;


So...

Set y1 to the slope of line 1 plus the intercept of line1.
Set y2 to the slope of line 2 plus the intercept of line2.
Then throw away the value of that second step and set y2 equals to the same value as y1.

That doesn't look even remotely related to finding the intersection of two lines.

And this:


That says that your intersection method returns a single number. You do know that the intersection will be a point, right, and that a point has 2 coordinates?

Honestly, it seems like you're just taking random bits of code and throwing them together and hoping that will magically produce the result. I'll say it again: You need to thoroughly understand the problem without any regard to Java at all, and you need to be able to describe your solution clearly and precisely in English and/or pseudocode before writing a line of Java.

So, given two lines, one described by slope m1 and intercept c1, and the other described by slope m2 and intercept c2, how do you find the point of intersection of those lines, without Java?

fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

in math term: if i had an equation solve for a single variable, and then plug it in to the found variable and solve for the other one. then use those found and plug them into the other. but they are all variable. i could move everything onto one side but outside of that, what else can i do?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred perez wrote:in math term: if i had an equation solve for a single variable, and then plug it in to the found variable and solve for the other one. then use those found and plug them into the other. but they are all variable.


That's very far from precise. I would never be able to write code from that description

i could move everything onto one side but outside of that, what else can i do?


That's exactly what you should do.

As I already said, you know that the end result of your method needs to be a point right? And that it will have an X and a Y coordinate?

So, again, given m1 and c1 to define one line and m2 and c2 to define the second line, what are the equations to solve for the X and Y coordinates of the intersection?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
You also may not be able to return a double; you might have to create a Point class to encapsulate both x and y.
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

is this how you put the equ. equal to each other?

y1 = m1 +c1

y2 = m2 + c2

Point intersection(int m1, int m2, int c1, int c2){
int x = 0;
int y = 0;
x = (c2 - c1) / (m1 - m2);
y = m2 * x + c2;
return new Point(x,y);
}
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred perez wrote:is this how you put the equ. equal to each other?

y1 = m1 +c1

y2 = m2 + c2

Point intersection(int m1, int m2, int c1, int c2){
int x = 0;
int y = 0;
x = (c2 - c1) / (m1 - m2);
y = m2 * x + c2;
return new Point(x,y);
}


What happened when you tested it with various values that you also worked out by hand? Did it give the correct results?
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

it compile okay, but when i tried to run it, it said i need a main class so i made one and it said it was missing a symbol "; "
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred perez wrote:it compile okay, but when i tried to run it, it said i need a main class so i made one and it said it was missing a symbol "; "


Then you'll have to fix that I guess. You weren't planning on handing it in without testing it anyway, were you?
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

please help me with this code. i have been at this for the last 2 day and i am having nightmares. is this code right?



package intersection;

import java.awt.Point;


public class Intersection {

int x = 0;
int y = 0;
public static void main(String args[]){

Point intersection(int m1,int m2, int c1, int c2)
{

int x = 0;
int y = 0;

x = (c2 - c1) / (m1 - m2);
y = (m2 * x + c2);
}
return new Point(x,y);

}








Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred perez wrote:please help me with this code. i have been at this for the last 2 day and i am having nightmares. is this code right?


I'm not going to give you that answer. It would be ridiculous to hand in code without testing it, so get your test program working. If what's stopping you is a simple compile error, then you definitely need the practice anyway.

For that matter, does the code you posted even compile? It doesn't look like it will. If it doesn't compile, then obviously it's not right.
fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

this is my last attempt to write this code because this is due tonight by midnight. can anyone tell me if i am close or should give up? i have studied for loops and ifs statement and array and have all the individual parts but cant put it all together. help please!



[code=java][
package intersection;


public class Intersection {
float point [];
float x = 0;
float y = 0;
public static void main(String args [] ){

}
public void intersection ( float m1,float m2, float c1, float c2)
{
float x1 [] = new float [2];
point [0] = x;
point [1] = y;

x = (c2 - c1) / (m1 - m2);
y = (m2 * x + c2);

for ( float line : x1){
System.out.print(point[0] + point[1]);
}

}
}








]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Why do you continue to refuse to test your code? If you want to know if code is correct, testing it is an absolute MUST. Your assignment even says, "Once completed, perform unit testing of your routine..."

And why do you have a main() method with nothing in it?

fred perez
Greenhorn

Joined: Mar 03, 2012
Posts: 14

i toke out the system.out.println
and ran a junit test and it gave me a bunch of error that i cant not understand. what i am suppose to do with that? it failed, now what?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
fred perez wrote:i toke out the system.out.println
and ran a junit test and it gave me a bunch of error that i cant not understand. what i am suppose to do with that? it failed, now what?

Change the code so that you don't get errors and it doesn't fail.
Unless you TellTheDetails that's about the best anyone can tell you.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding the intersection of two lines- for a junit test case