Hi, ive got to make a program to find the perimeter and area of a triangle made using vectors, and i have not a clue, ill post the code i have done so far and if any1 could give me the code for the area and perimeter you would be life savers, thanks in advance.

public class Tri{

	private Point VectorA;
	private Point VectorB;
	private Point VectorC;
	private Point VectorD;
	private double perimeter;
	private double area;
	
	public Tri(Point VectorA, Point VectorB, Point VectorC){
		this.VectorA=VectorA;
		this.VectorB=VectorB;
		this.VectorC=VectorC;
		this.VectorD=VectorD;
		calculateperimeter();
		calculatearea();
		}
	
	public void setVectorA (Point VectorA){
		this.VectorA=VectorA;
		calculateperimeter();
		}
		
	public Point getVectorA(){
		return VectorA;
	}
	
	public void setVectorB (Point VectorB){
		this.VectorB=VectorB;
		calculateperimeter();
		}
		
	public Point getVectorB(){
		return VectorB;
	}
	
	public void setVectorC (Point VectorC){
		this.VectorC=VectorC;
		calculateperimeter();
		}
	public Point getVectorC(){
		return VectorC;
	}
	
	public void setVectorD (Point VectorD){
		this.VectorD=VectorD;
		calculateperimeter();
		}
		
	public Point getVectorD(){
		return VectorD;
	}
	
	private void calculateperimeter(){
		int perimeterx = VectorA.getX()-VectorB.getX();
		int perimetery = VectorC.getY()-VectorD.getY();
		perimeter=Math.sqrt ((perimeterx * perimeterx) + (perimetery * perimetery));
	}
	
	public double getperimeter(){
		return perimeter;
		}
		
	private void calculatearea(){
		int areax = VectorA.getX()-VectorB.getX();
		int areay = VectorC.getY()-VectorD.getY();
		area=Math.sqrt ((area * area) + (areax * areay));
	}
	
	public double getarea(){
		return area;
		}

}

as you can see the code is from my rectangle version of this program so it doesnt work completely :S

Recommended Answers

All 8 Replies

Its refreshing to see that some posters pay attention to atleast some of the rules before posting, but we wont give you ready made code, but we'll help you write it yourself.

help is fine lol,even a hint in the right direction will be great :D i just need the basic idea, and i should be able to sort it out. thanks for pointing out the [ code ] thing aswel :)

Now let us consider your traingle has three vertices
A (x1, y1)
B (x2, y2)
C (x3, y3).

Now if you want to calculate the distance between any two points ex P(x1,y1) and Q (x2, y2) on a plane the formula is:-
Square_Root [(x2-x1)^2 + (y2-y1)^2]

So using that formula you can get the length of all the three sides of the triangle. Next to calculate the Area of the triangle Whose length of sides are a,b and c the formula is :-

Area = Square_Root [s(s-a)(s-b)(s-c)]
where s -> Semiperimeter that is half the perimeter.

I think this is called the Herons Formula or something similar.

Now I have given you the formulae just convert them to your code.

Plus are you sure the code you have pasted is the working code ?? Cause the area of a rectangle is (base (areax) * height(areay)) why do you have "(area * area)" added to it ??? Are you sure about what you are doing here ??
Also your program would fail if the rectangle is not perfectly horizontal that is the base is inclined at some angle with the X-Axis.

I hope this is good enough to get you on the way.

<Edit> I think you code is currently working cause since the variable area is declared at the class level, it is by default initialized to 0, hence (area * area) would give you just 0, but if this were C++ you would have found it out earlier as it would give you garbage values.
Also another suggestion would be to drop the IM or SMS speech while posting, type the words fully so we do not have to figure out what you are actually trying to say.

thanks, ill get started on the formula, hope i cn get it working :P

and the rectangle code got me 49/50 marks for a uni assignment so im supposing its all wrking, he probably just overlooked the (area * area) part as it didnt affect the overall program.

ok ive done some code and i think it is mostly right, but im getting 3 errors with the area part.

public class Tri{

	private Point VectorA;
	private Point VectorB;
	private Point VectorC;
	private Point VectorD;
	private double perimeter;
	private double area;

	
	public Tri(Point VectorA, Point VectorB, Point VectorC){
		this.VectorA=VectorA;
		this.VectorB=VectorB;
		this.VectorC=VectorC;
		this.VectorD=VectorD;
		calculateperimeter();
		calculatearea();
		}
	
	public void setVectorA (Point VectorA){
		this.VectorA=VectorA;
		calculateperimeter();
		}
		
	public Point getVectorA(){
		return VectorA;
	}
	
	public void setVectorB (Point VectorB){
		this.VectorB=VectorB;
		calculateperimeter();
		}
		
	public Point getVectorB(){
		return VectorB;
	}
	
	public void setVectorC (Point VectorC){
		this.VectorC=VectorC;
		calculateperimeter();
		}
	public Point getVectorC(){
		return VectorC;
	}
	
	public void setVectorD (Point VectorD){
		this.VectorD=VectorD;
		calculateperimeter();
		}
		
	public Point getVectorD(){
		return VectorD;
	}

	private void calculateperimeter(){
		int perimeterx = VectorA.getX()-VectorB.getX();
		int perimetery = VectorB.getY()-VectorC.getY();
		int perimeterz = VectorC.getX()-VectorA.getX();
		perimeter=Math.sqrt ((perimeterx * perimeterx) + (perimetery * perimetery) + (perimeterz * perimeterz));
	}
	
	public double getperimeter(){
		return perimeter;
		}
		
	private void calculatearea(){
		area=Math.sqrt (getperimeter() / 2
		* (perimeter / 2 - perimeterx)
			* (perimeter / 2 - perimetery)
			* (perimeter / 2 - perimeterz));
	}
	
	public double getarea(){
		return area;
		}

}

and the errors are Tri.java:68: cannot find symbol
symbol : variable perimeterx
location: class Tri
* (perimeter / 2 - perimeterx)
^
Tri.java:69: cannot find symbol
symbol : variable perimetery
location: class Tri
* (perimeter / 2 - perimetery)
^
Tri.java:70: cannot find symbol
symbol : variable perimeterz
location: class Tri
* (perimeter / 2 - perimeterz));

any advice is great thanks.

That's because you only declared those variables locally in the calculateperimeter() method and they have no scope in your calculatearea() method.

thanks alot everyone, ive got it working now :) Thanks for all the help.

I do not know what theheck you have done here, but it seems plain wrong to me.

private void calculateperimeter(){
		int perimeterx = VectorA.getX()-VectorB.getX();
		int perimetery = VectorB.getY()-VectorC.getY();
		int perimeterz = VectorC.getX()-VectorA.getX();
		perimeter=Math.sqrt ((perimeterx * perimeterx) + (perimetery * perimetery) + (perimeterz * perimeterz));
	}

If your instructor is ignorant enough this time too I guess you will get away with this also. According to me that is completely the wrong way to calculate the perimeter.

I had already given you the formula for calculating the length of the sides of the triangle (Square_Root [(x2-x1)^2 + (y2-y1)^2]) from which you could calculate the perimeter and then the area.

But to me this appears plain wrong it will give you the incorrect perimeter and will also finally give you a wrong answer for the area of your triangle.

BTW if you do not know what a perimeter is, it is supposed to be the length of the shape, so for the triangle it would be the sum of the length of all the three sides, But honestly such things are basic geometry you should know about.

<EDIT>
Also a word of advice, do not simply program to get the marks, Soon no one is going to care whether you got 50 on 50 or 49 on 50 for a kiddy assignment, do it to learn and honestly from the way you have done both your assignments your attitude is nowhere near that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.