A friendly place for programming greenhorns!
Big Moose Saloon
Search

Java FAQ

Recent Topics

Flagged Topics

Hot Topics

Zero Replies
Register / Login
Win a copy of
Meteor in Action
this week in the
JavaScript
forum!
JavaRanch
»
Java Forums
»
Java
»
Applets
Author
Need help with triangle drawing applet
Justin Smythe
Greenhorn
Joined: Mar 30, 2005
Posts: 1
posted
Mar 30, 2005 08:58:00
0
I have code for a right traingle that you can input information and it gives back the rest and now I need to make it so that it will draw the triangle at the end, how can I do this?
here's the code: Yes, it's long
//************************************************************************ // // trig.java // // Input known information about a triangle and it give back the rest // well, it should //********************************************************************** import java.math.*; import cs1.Keyboard; import java.text.DecimalFormat; public class trig { public static DecimalFormat deci = new DecimalFormat ("0.##"); public static final double C = .5; public static double a, b, c, tanA, cosH, sineH; public static double A, B, cosS, sineS, tanS, cosA, sineA; public static final double pi = 3.141592653589793; public static void triDesc() { //Triangle Picture Curtesy of Andy System.out.println(" B"); System.out.println(" \\"); System.out.println("  \\"); System.out.println("  \\"); System.out.println("  \\"); System.out.println("a \\c"); System.out.println("  \\"); System.out.println("  \\"); System.out.println("  \\"); System.out.println("  \\"); System.out.println(" _________\\"); System.out.println(" C b A"); } // //Input Methods //Get side a public static double sideInputA() { System.out.println("Please input side a. Input 0 if unknown."); a = Keyboard.readDouble(); return a; } //Get side b public static double sideInputB() { System.out.println("Please input side b. Input 0 if unknown."); b = Keyboard.readDouble(); return b; } //Get side c public static double sideInputC() { System.out.println("Please input side c. Input 0 if unknown."); c = Keyboard.readDouble(); return c; } //Get angle A public static double angleInputA() { System.out.println("Please input angle A in degrees. Input 0 if unknown."); A = Keyboard.readDouble(); A *= pi/180; return A; } //Get angle b public static double angleInputB() { System.out.println("Please input angle B in degrees. Input 0 if unknown."); B = Keyboard.readDouble(); B *= pi/180; return B; } // // Find Base Sides public static double findSideA() { return a = Math.sqrt(c*cb*b); } public static double findSideB() { return b = Math.sqrt(c*ca*a); } public static double cosSide(double hyp, double angle) { cosS = hyp * Math.cos(angle); return cosS; } public static double tanOSide(double side, double angle) { tanS = side * Math.tan (angle); return tanS; } public static double tanASide(double side, double angle) { tanS = side / Math.tan (angle); return tanS; } public static double sineSide(double hyp, double angle) { sineS = hyp * Math.sin(angle); return sineS; } // // // Find HYP public static double findSideC() { return c = Math.sqrt(a*a+b*b); } public static double cosHyp(double side, double angle) { cosH = Math.cos(angle) / side; return cosH; } public static double sineHyp(double side, double angle) { sineH = Math.sin(angle) / side; return sineH; } // //Find the Angles //Know 2 angles public static double findAngleA() { return A = .5 * pi  B; } public static double findAngleB() { return B = pi / 2  A; } //Know 1 angle public static double sineAngle(double opp, double hyp) { sineA = opp / hyp; return sineA; } public static double cosAngle(double adj, double hyp) { cosA = adj / hyp; return cosA; } public static double tanAngle(double opp, double adj) { tanA = opp / adj; return tanA; } public static void main (String[] args) { triDesc(); //Inputs sideInputA(); sideInputB(); sideInputC(); angleInputA(); angleInputB(); //Find the unknown side if nessercery //************************************ //Find the A //Only missing a if ((a == 0.0) && (b != 0.0) && (c != 0.0)) findSideA(); //Know c and A else if ((a == 0.0) && (b == 0.0) && (c != 0.0) && ( A != 0.0)) { sineSide(c, A); a = sineS; } //Know b and A else if ((a == 0.0) && (b != 0.0) && (c == 0.0) && ( A != 0.0)) { tanOSide(b, A); a = tanS; } //Know c and B else if ((a == 0.0) && (b == 0.0) && (c != 0.0) && ( B != 0.0)) { cosSide(c, B); a = cosS; } //Know b and B else if ((a == 0.0) && (b != 0.0) && (c == 0.0) && ( B != 0.0)) { tanASide(b, B); a = tanS; } //************************************ //Find b //Only missing b if ((a != 0.0) && (b == 0.0) && (c != 0.0)) findSideB(); //Know c and A else if ((b == 0.0) && (a == 0.0) && (c != 0.0) && ( A != 0.0)) { cosSide(c, A); b = cosS; } //Know a and A else if ((b == 0.0) && (a != 0.0) && (c == 0.0) && ( A != 0.0)) { tanASide(a, A); b = tanS; } //Know c and B else if ((b == 0.0) && (a == 0.0) && (c != 0.0) && ( B != 0.0)) { sineSide(c, B); b = sineS; } //Know a and B else if ((b == 0.0) && (a != 0.0) && (c == 0.0) && ( B != 0.0)) { tanOSide(a, B); b = tanS; } //************************************ //Find c //Know a and b if ((a != 0.0) && (b != 0.0) && (c == 0.0)) findSideC(); //know a and A else if ((c == 0.0) && ( a != 0) && (b == 0 ) && (A != 0)) { sineHyp(a, A); c = sineH; } //know a and B else if ((c == 0.0) && ( a != 0) && (b == 0 ) && (B != 0)) { cosHyp(a, B); c = cosH; } //know b and A else if ((c == 0.0) && ( a == 0) && (b != 0 ) && (A != 0)) { cosHyp(b, A); c = cosH; } //know b and B else if ((c == 0.0) && ( a == 0) && (b != 0 ) && (B != 0)) { sineHyp (b, B); c = sineH; } //************************************ //************************************ //FIND ANGLES //************************************ //Find A //know B if ((A == 0) && (B != 0)) findAngleA(); //know no angles except C //know a and c else if ((A == 0) && (B == 0) && (a != 0) && (c != 0)) { sineAngle(a, c); A = sineA; } //know b and c else if ((A == 0) && (B == 0) && (b != 0) && (c != 0)) { cosAngle(b, c); A = cosA; } //know a and b else if ((A == 0) && (B == 0) && (a != 0) && (b != 0)) { tanAngle(a, b); A = tanA; } //********************************** //Find B //know A if ((B==0) && (A != 0)) findAngleB(); //know no angles except C //know a and c else if ((B==0) && (A == 0) && (a != 0) && (c != 0)) { cosAngle(a, c); B = cosA; } //know b and c else if ((B==0) && (A == 0) && (b != 0) && (c != 0)) { sineAngle(b, c); B = sineA; } //know a and b else if ((B==0) && (A == 0) && (a != 0) && (b != 0)) { tanAngle(b, a); B = tanA; } //********************************** //Print Sides and Angles System.out.println ("Side a = " + deci.format(a)); System.out.println ("Side b = " + deci.format(b)); System.out.println ("Side c = " + deci.format(c)); System.out.println ("Angle A = " + deci.format(((A * 180) / pi))); System.out.println ("Angle B = " + deci.format(((B * 180) / pi))); System.out.println ("Angle C = " + deci.format((((C * pi) * 180) / pi))); } }
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
posted
Mar 30, 2005 15:09:00
0
private static void drawTriangle() { String s; for(int j = 1; j <= a; j++) { s = getNextLine(j); System.out.println(s); } s = " "; for(int j = 0; j < b; j++) s += ""; System.out.println(s); } private static String getNextLine(int y) { int width = (int)((y * b)/a); String s = " "; for(int j = 0; j < width; j++) s += " "; s += "\\"; return s; }
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2367
posted
Mar 30, 2005 17:58:00
0
You can also use drawPolygon
I agree. Here's the link:
http://aspose.com/filetools
subject: Need help with triangle drawing applet
Similar Threads
Polygon Rotation
Passing Obj Ref vs value and primitives
Quadratic perplextion
how do i rotate a point from a center point??
Losing double values in a Point[ ] array
All times are in JavaRanch time: GMT6 in summer, GMT7 in winter