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

posted 4 years ago

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");

}

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

Posts: 50

posted 4 years ago

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.

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.

posted 4 years ago

But look at these two lines:

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

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?

posted 4 years ago

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.

greatly appreciate help!

posted 4 years ago

I have no idea what you're saying here.

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

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.

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.

posted 4 years ago

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.

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.

greatly appreciate help!

posted 4 years ago

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

And have you figured out how to find the intersection

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.

posted 4 years ago

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");

}

]

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");

}

]

greatly appreciate help!

posted 4 years ago

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.)

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

Posts: 50

posted 4 years ago

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 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.

regards,

Victor M. Pereira

Victor M. Pereira

Ranch Hand

Posts: 50

posted 4 years ago

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

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

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 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?

posted 4 years ago

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?

greatly appreciate help!

posted 4 years ago

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

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,

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

Posts: 51349

87

posted 4 years ago

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

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?

posted 4 years ago

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);

}

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);

}

greatly appreciate help!

posted 4 years ago

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 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.

posted 4 years ago

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]);

}

}

}

]

[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]);

}

}

}

]

greatly appreciate help!

posted 4 years ago

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?

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?

greatly appreciate help!

Joanne Neal

Rancher

Posts: 3742

16

posted 4 years ago

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.

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