Hey Guys check out this code !!!! get two points and return the line !! checked for all occurrences !!!! Please tell me if there are any errors !!!!!!
Happy Codings !!!!! For More Java And C++ codes Visit
http://www.elakiri.com/forum/showthread.php?t=245895
Code:
package line;
import java.util.Scanner;
/**
*
* @author lakmal
*/
class Line {
Points p1=new Points();
Points p2=new Points();
static boolean isContinue=true;
static Scanner input=new Scanner(System.in);
static String op;
void getLine(Points p1,Points p2){
System.out.println("Your two points are ");
System.out.println("("+p1.xCordinate+","+p1.yCordinate+")");
System.out.println("("+p2.xCordinate+","+p2.yCordinate+")");
double yCoeff=p2.xCordinate-p1.xCordinate;
double xCoeff=p2.yCordinate-p1.yCordinate;
double constant=p1.yCordinate*(p2.xCordinate-p1.xCordinate)-p1.xCordinate*(p2.yCordinate-p1.yCordinate);
System.out.println("Your Line is:");
if (yCoeff==0){
System.out.println("X="+p2.xCordinate);
}
else if (xCoeff==0)
System.out.println("Y="+p2.yCordinate);
else{
if (constant!=0){
if ((constant/yCoeff)>0){
op = "+";
}
else if ((constant/yCoeff)<0){
op="";
}
System.out.println("Y="+xCoeff/yCoeff+"X" + op + (constant)/yCoeff);
}
else
System.out.println("Y="+ xCoeff/yCoeff+"X");
}
}
void getPoints(){
System.out.println("Enter your First Point:");
System.out.print("X=");
this.p1.xCordinate=input.nextInt();
System.out.print("Y=");
this.p1.yCordinate=input.nextInt();
System.out.println("Enter your Second Point:");
System.out.print("X=");
this.p2.xCordinate=input.nextInt();
System.out.print("Y=");
this.p2.yCordinate=input.nextInt();
this.getLine(this.p1,this.p2);
}
public static void main(String[] args) {
do {
Line l1=new Line();
l1.getPoints();
System.out.println("Do you want to Continuw with anothe line:(y/n)");
char state=input.next().charAt(0);
if (state=='n')
isContinue=false;
} while (isContinue=true);
}
}
class Points {
int xCordinate;
int yCordinate;
}
Happy Codings !!!!! For More Java And C++ codes Visit
http://www.elakiri.com/forum/showthread.php?t=245895