|
|
|
#1 (permalink) | ||||||||||||||
|
Intel Overclocker
|
Whoo! For class i had to make a triangle class which takes in either SSS or SAS and calculates the other sides and angles. I finally did it.
get and set methods for all instance variables as well as getArea and drawTriangle. Code:
import TurtleGraphics.StandardPen;
public class Triangle
{
private double base, side2, side3, side1;
private double height, angle1, angle2, angle3;
//SAS works!
public Triangle(double s1, double a3, double s2, String sas)
{
side1 = s1;
side2 = s2;
angle3= a3;
side3 = Math.sqrt((s1*s1)+(s2*s2)-2*s1*s2*(Math.cos(Math.toRadians(a3))));
angle2=Math.toDegrees(Math.asin(s2*(Math.sin(Math.toRadians(a3)))/side3));
angle1=180-(angle2+angle3);
base=0;
height=0;
}
//It works!
public Triangle(double s1, double s2, double s3)
{
side1 = s1;
side2 = s2;
side3 = s3;
angle1= Math.toDegrees(Math.acos( ((s1*s1)-(s2*s2)-(s3*s3))/(-2*s2*s3) ));
angle2= Math.toDegrees(Math.acos( ((s2*s2)-(s1*s1)-(s3*s3))/(-2*s1*s3) ));
angle3= Math.toDegrees(Math.acos( ((s3*s3)-(s1*s1)-(s2*s2))/(-2*s1*s2) ));
}
public double getBase()
{
return base;
}
public double getSide1()
{
return side1;
}
public double getSide2()
{
return side2;
}
public double getSide3()
{
return side3;
}
public double getHeight()
{
return height;
}
public double getAngle1()
{
return angle1;
}
public double getAngle2()
{
return angle2;
}
public double getAngle3()
{
return angle3;
}
public double getArea()
{
double s = 0.5*side1*side2*side3;
double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
public void drawTriangle()
{
double sideOne = this.side1;
double sideTwo = this.side2;
double sideThree = this.side3;
double angleOne = this.angle1;
double angleTwo = this.angle2;
double angleThree = this.angle3;
StandardPen pen = new StandardPen();
pen.move((int)sideOne*100);
pen.turn(180-angleThree);
pen.move((int)sideTwo*100);
pen.turn(180-angleOne);
pen.move((int)sideThree*100);
}
public String toString()
{
String str;
str = "side1 = " +side1+ " \n" + "side2 = " +side2+ " \n" + "side3 = " +side3+ " \n" +"angle1 = " +angle1+ " \n" +"angle2 = " +angle2+ " \n" +"angle3 = " +angle3+ " \n" +"base = " +base+ " \n" +"height = " +height+ " \n";
return str;
}
}
__________________
Quote:
SLi Zone PSU listing- check to see if your PSU is SLi certified. BF2 stats BF2142 stats My Guide to BF2 Unlocks
Last edited by cgrado : 04-15-07 at 12:28 AM. |
||||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Jack of all trades
Join Date: Mar 2007
Location: Cambridge, Ontario
Posts: 3,563
Rep: 271
![]() ![]() ![]() Unique Rep: 208
Trader Rating: 2
|
very nice, I love the detail in your PI, too much superpi?
__________________
I am 91% addicted to Counterstrike. What about you? ![]() OCN CS:S GG: overclock.nuclearfallout.net:27015 OCN TF2: 8.9.16.94:27015 (temporary) [CSS GUN GAME ADMIN] Ban Hamma of Pwnage +5 skillz [TF2 ADMIN] Ban Hammer of scouting + 20 scout powaz
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||||
|
Intel Overclocker
|
haha, i didn't even use it though. i just had it for some checking purposes against what the Math class uses. i should probably take it out.
__________________
Quote:
SLi Zone PSU listing- check to see if your PSU is SLi certified. BF2 stats BF2142 stats My Guide to BF2 Unlocks
|
||||||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||
|
110100001101001111000
|
For computing area, getting the height would be fairly easy if you set up your sides properly. Start with a reference side. Subtract it's angle from all the sides, so you ensure that the reference side is horizontal, but the shape remains the same. Since the reference side is now horizontal, you know that you can trace either of the remaining sides to find the top point of the triangle. Your height is then simply h = length of trace side*sin(angle of trace side). Since you already rotated the reference side horizontal, the magnitude of your reference side gives you your base. And there you have all the elements for the area. Sound kind of confusing, but if you draw out a triangle on paper it becomes much more intuitive.
__________________
|
||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Intel Overclocker
|
thanks!
__________________
Quote:
SLi Zone PSU listing- check to see if your PSU is SLi certified. BF2 stats BF2142 stats My Guide to BF2 Unlocks
|
||||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
There is an easier way. IIRC
d = sqrt(a^2+b^2+c^2) A = sqrt((d-a)^2+(d-b)^2+(d-c)^2) I'd look that up to chekc it
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
110100001101001111000
|
Quote:
d = 0.5*(a+b+c); // this is called the semiperimeter A = sqrt((d*(d-a)*(d-b)*(d-c));
__________________
|
|||||||||||||
|
|
|
|
#8 (permalink) | ||||||||||||||
|
Intel Overclocker
|
Thanks, c-bro, i used that. I'll update the code.
__________________
Quote:
SLi Zone PSU listing- check to see if your PSU is SLi certified. BF2 stats BF2142 stats My Guide to BF2 Unlocks
|
||||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
..the party can start now
|
dude...it's called trigonometry? lol...or am I missing something?
__________________
|
|||||||||||||
|
|
|
|
#10 (permalink) | ||||||||||||||
|
Rabble rabble
Join Date: Oct 2006
Location: 29 Palms(stumps), CA
Posts: 2,305
Rep: 134
![]() ![]() Unique Rep: 108
Trader Rating: 3
|
Heh not bad man. I'm almost done with AP computer science and I'm still confused on a LOT of the Java language.
__________________
Quote:
Overclock.net TOS: Live it. Love it.![]() THE CHEVY Owners CLUB!!!
|
||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|