البرنامج يأخذ 6 مدخلات والتي تمثل الإحداثايات الخاصة بنقاط المثلث
كود
(x1,y1)
(x2,y2)
(x3,y3)
(x2,y2)
(x3,y3)
العمليات التي قمت بتصميمها
أ- هل الشكل يمثل مثلث
بالطبع النقاط الثلاث تمثل مثلت إلا في حالتين
1- تكرار نقطة
كأن تكون النقطة الثانية والثالثة بنفس الإحداثيات
2- أن تكون الثلاث نقاط على خط واحد
ب- أطوال الأضلاع
واستخدمت في هذه العملية المسافة الإقليدية لحساب الأضلاع
حسب المعادة التالية
إذا كان x يمثل المسافة بين النقطة الأولى والثانية
كود
x=sqrt(pow(x1-x2,2)+pow(y1-y2))
ج- تصنيف المثلث إذا كان متطابق الضلعين
في هذه العملية أقوم بفحص الضلع
أقوم باستخدام العملية السابقة
وأقارن بين أطوال الأضلاع
د- تصنيف المثلثات على حسب زواياه
المثلث ينقسم على حسب زواياه إلى ثلاثة أقسام
إذا كان يحتوي على زاوية منفرجة فهو منفرج
إذا كان يحتوي على زاويه قائمة فهو قائم
وإلا فهو حاد
يكون المثلث منفرج
إذا كان مربع أحد الأضلاع أكبر من مبربعي الضلعين الآخرين
يكون المثلث قائم (نظرية فيثاغورث)
إذا كان مربع أحد الأضلاع يساوي مربعي الضلعين الآخرين
ويكون حاد إذا لم يتوفر أحد الشرطين السابقين
البرنامج يأخذ المدخلات من شاشة الدوس
ويأخذ أي عدد من المثلثات
بالطبع كل مثلث يتمثل في 6 مدخلات
مثال على المدخلات
0 0 0 4 1 2
1 1 1 4 3 2
2 2 2 4 4 3
3 3 3 4 5 3
4 4 4 5 5 6
5 5 5 6 6 5
6 6 6 7 6 8
7 7 7 7 7 7
أترككم مع الشيفرة
كود
package triangle;
public class Triangle {
private double x1;
private double x2;
private double x3;
private double y1;
private double y2;
private double y3;
public Triangle(double x1,double y1,double x2,double y2,double x3,double y3){
this.x1=x1;
this.x2=x2;
this.x3=x3;
this.y1=y1;
this.y2=y2;
this.y3=y3;
}
public boolean isTriangler() {
if(x1==x2&&y1==y2||x1==x3&&y1==y3||x3==x2&&y3==y2)//not distnict
return false;
double[] lengths=lengths();
if(lengths[0]==lengths[1]+lengths[2]||lengths[1]==lengths[0]+lengths[2]||lengths[2]==lengths[1]+lengths[0])//parallel
return false;
return true;
}
public double[] lengths() {
double lengths[]=new double[3];
lengths[0]=Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
lengths[1]=Math.sqrt(Math.pow(x2-x3, 2)+Math.pow(y2-y3, 2));
lengths[2]=Math.sqrt(Math.pow(x3-x1, 2)+Math.pow(y3-y1, 2));
return lengths;
}
public String isScalene() {
double lengths[]=lengths();
if(lengths[0]!=lengths[1]&&lengths[1]!=lengths[2]&&lengths[0]!=lengths[2])
return "scalene";
else
return "isosceles";
}
public String classify() {
double lengths[]=lengths();
double A=Math.floor(Math.pow(lengths[0], 2)+.00005);
double B=Math.floor(Math.pow(lengths[1], 2)+.00005);
double C=Math.floor(Math.pow(lengths[2], 2)+.00005);
double D=Math.floor(Math.pow(lengths[1], 2)+Math.pow(lengths[2], 2)+.00005);
double E=Math.floor(Math.pow(lengths[0], 2)+Math.pow(lengths[2], 2)+.00005);
double F=Math.floor(Math.pow(lengths[1], 2)+Math.pow(lengths[0], 2)+.00005);
System.out.println(A+"="+D);
System.out.println(B+"="+E);
System.out.println(C+"="+F);
if(A==D||B==E||C==F)
return "right";
else if(Math.pow(lengths[0], 2)>Math.pow(lengths[1], 2)+Math.pow(lengths[2], 2)
||Math.pow(lengths[1], 2)>Math.pow(lengths[0], 2)+Math.pow(lengths[2], 2)
||Math.pow(lengths[2], 2)>Math.pow(lengths[1], 2)+Math.pow(lengths[0], 2))
return "obtuse";
else
return "acute";
}
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
double x1=Double.parseDouble(args[i]);
i++;
double y1=Double.parseDouble(args[i]);
i++;
double x2=Double.parseDouble(args[i]);
i++;
double y2=Double.parseDouble(args[i]);
i++;
double x3=Double.parseDouble(args[i]);
i++;
double y3=Double.parseDouble(args[i]);
Triangle t=new Triangle(x1,y1,x2,y2,x3,y3);
double[] lengths=t.lengths();
for (int j = 0; j < lengths.length; j++) {
System.out.println(lengths[j]+" ");
}
System.out.println();
if(t.isTriangler()){
StringBuilder s=new StringBuilder(" triangle");
s.insert(0," " + t.classify());
s.insert(0,t.isScalene());
System.out.println(s.toString());
}
else
System.out.println("not a triangle");
}
}
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public double getX3() {
return x3;
}
public void setX3(double x3) {
this.x3 = x3;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public double getY3() {
return y3;
}
public void setY3(double y3) {
this.y3 = y3;
}
}
public class Triangle {
private double x1;
private double x2;
private double x3;
private double y1;
private double y2;
private double y3;
public Triangle(double x1,double y1,double x2,double y2,double x3,double y3){
this.x1=x1;
this.x2=x2;
this.x3=x3;
this.y1=y1;
this.y2=y2;
this.y3=y3;
}
public boolean isTriangler() {
if(x1==x2&&y1==y2||x1==x3&&y1==y3||x3==x2&&y3==y2)//not distnict
return false;
double[] lengths=lengths();
if(lengths[0]==lengths[1]+lengths[2]||lengths[1]==lengths[0]+lengths[2]||lengths[2]==lengths[1]+lengths[0])//parallel
return false;
return true;
}
public double[] lengths() {
double lengths[]=new double[3];
lengths[0]=Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
lengths[1]=Math.sqrt(Math.pow(x2-x3, 2)+Math.pow(y2-y3, 2));
lengths[2]=Math.sqrt(Math.pow(x3-x1, 2)+Math.pow(y3-y1, 2));
return lengths;
}
public String isScalene() {
double lengths[]=lengths();
if(lengths[0]!=lengths[1]&&lengths[1]!=lengths[2]&&lengths[0]!=lengths[2])
return "scalene";
else
return "isosceles";
}
public String classify() {
double lengths[]=lengths();
double A=Math.floor(Math.pow(lengths[0], 2)+.00005);
double B=Math.floor(Math.pow(lengths[1], 2)+.00005);
double C=Math.floor(Math.pow(lengths[2], 2)+.00005);
double D=Math.floor(Math.pow(lengths[1], 2)+Math.pow(lengths[2], 2)+.00005);
double E=Math.floor(Math.pow(lengths[0], 2)+Math.pow(lengths[2], 2)+.00005);
double F=Math.floor(Math.pow(lengths[1], 2)+Math.pow(lengths[0], 2)+.00005);
System.out.println(A+"="+D);
System.out.println(B+"="+E);
System.out.println(C+"="+F);
if(A==D||B==E||C==F)
return "right";
else if(Math.pow(lengths[0], 2)>Math.pow(lengths[1], 2)+Math.pow(lengths[2], 2)
||Math.pow(lengths[1], 2)>Math.pow(lengths[0], 2)+Math.pow(lengths[2], 2)
||Math.pow(lengths[2], 2)>Math.pow(lengths[1], 2)+Math.pow(lengths[0], 2))
return "obtuse";
else
return "acute";
}
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
double x1=Double.parseDouble(args[i]);
i++;
double y1=Double.parseDouble(args[i]);
i++;
double x2=Double.parseDouble(args[i]);
i++;
double y2=Double.parseDouble(args[i]);
i++;
double x3=Double.parseDouble(args[i]);
i++;
double y3=Double.parseDouble(args[i]);
Triangle t=new Triangle(x1,y1,x2,y2,x3,y3);
double[] lengths=t.lengths();
for (int j = 0; j < lengths.length; j++) {
System.out.println(lengths[j]+" ");
}
System.out.println();
if(t.isTriangler()){
StringBuilder s=new StringBuilder(" triangle");
s.insert(0," " + t.classify());
s.insert(0,t.isScalene());
System.out.println(s.toString());
}
else
System.out.println("not a triangle");
}
}
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public double getX3() {
return x3;
}
public void setX3(double x3) {
this.x3 = x3;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public double getY3() {
return y3;
}
public void setY3(double y3) {
this.y3 = y3;
}
}
كالعادة يمكنك التعديل عليه كما تشاء
أو طرح أي أفكار لديك
إذا كان لديك فكرة عن طريقة أسهل لعمل أي دالة
فناقشها ليستفيد الجميع
تحياتي






