先创建一个Point类,然后定义Triangle类。在Triangle类中定义三个Point的对象用来表示一个三角形的三个点,再定义一个方法setTri对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。在main()中创建一个三角形对象,求给定三点的三角形的周长、面积
时间: 2023-08-31 19:40:38 浏览: 111
先创建一个Point类,然后定义Trianglele类
4星 · 用户满意度95%
### 回答1:
下面是代码实现:
```python
import math
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
class Triangle:
def __init__(self):
self.p1 = Point()
self.p2 = Point()
self.p3 = Point()
def setTri(self, x1, y1, x2, y2, x3, y3):
self.p1.x = x1
self.p1.y = y1
self.p2.x = x2
self.p2.y = y2
self.p3.x = x3
self.p3.y = y3
def getSideLength(self, p1, p2):
return math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2)
def getPerimeter(self):
a = self.getSideLength(self.p1, self.p2)
b = self.getSideLength(self.p2, self.p3)
c = self.getSideLength(self.p3, self.p1)
return a + b + c
def getArea(self):
a = self.getSideLength(self.p1, self.p2)
b = self.getSideLength(self.p2, self.p3)
c = self.getSideLength(self.p3, self.p1)
s = (a + b + c) / 2
return math.sqrt(s * (s - a) * (s - b) * (s - c))
# 测试
t = Triangle()
t.setTri(0, 0, 3, 0, 0, 4)
print("周长:", t.getPerimeter())
print("面积:", t.getArea())
```
输出结果为:
```
周长: 12.0
面积: 6.0
```
### 回答2:
首先,我们需要创建一个Point类,这个类用来表示一个点的坐标。使用如下的方法创建Point类:
```python
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
```
然后,我们需要定义Triangle类,这个类用来表示一个三角形。Triangle类中有三个Point对象,用来表示三角形的三个点。我们使用如下的方法定义Triangle类:
```python
class Triangle:
def __init__(self):
self.point1 = Point(0, 0)
self.point2 = Point(0, 0)
self.point3 = Point(0, 0)
def setTri(self, x1, y1, x2, y2, x3, y3):
self.point1 = Point(x1, y1)
self.point2 = Point(x2, y2)
self.point3 = Point(x3, y3)
def getPerimeter(self):
# 计算三角形的周长
side1 = math.sqrt((self.point1.x - self.point2.x) ** 2 + (self.point1.y - self.point2.y) ** 2)
side2 = math.sqrt((self.point2.x - self.point3.x) ** 2 + (self.point2.y - self.point3.y) ** 2)
side3 = math.sqrt((self.point3.x - self.point1.x) ** 2 + (self.point3.y - self.point1.y) ** 2)
perimeter = side1 + side2 + side3
return perimeter
def getArea(self):
# 计算三角形的面积
side1 = math.sqrt((self.point1.x - self.point2.x) ** 2 + (self.point1.y - self.point2.y) ** 2)
side2 = math.sqrt((self.point2.x - self.point3.x) ** 2 + (self.point2.y - self.point3.y) ** 2)
side3 = math.sqrt((self.point3.x - self.point1.x) ** 2 + (self.point3.y - self.point1.y) ** 2)
s = (side1 + side2 + side3) / 2
area = math.sqrt(s * (s - side1) * (s - side2) * (s - side3))
return area
```
最后,在main()函数中,我们可以创建一个Triangle对象,然后调用setTri方法对三个点坐标进行初始化,并调用getPerimeter和getArea方法分别求解三角形的周长和面积,如下所示:
```python
import math
def main():
triangle = Triangle()
triangle.setTri(0, 0, 3, 0, 0, 4)
perimeter = triangle.getPerimeter()
area = triangle.getArea()
print("三角形的周长为:", perimeter)
print("三角形的面积为:", area)
if __name__ == "__main__":
main()
```
这样,我们就可以获得给定三个点的三角形的周长和面积了。
### 回答3:
Point类定义如下:
```java
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
```
Triangle类定义如下:
```java
public class Triangle {
private Point point1;
private Point point2;
private Point point3;
public void setTri(Point p1, Point p2, Point p3) {
point1 = p1;
point2 = p2;
point3 = p3;
}
public double getPerimeter() {
double side1 = calculateDistance(point1, point2);
double side2 = calculateDistance(point2, point3);
double side3 = calculateDistance(point3, point1);
return side1 + side2 + side3;
}
public double getArea() {
double side1 = calculateDistance(point1, point2);
double side2 = calculateDistance(point2, point3);
double side3 = calculateDistance(point3, point1);
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
private double calculateDistance(Point p1, Point p2) {
double xDiff = p2.getX() - p1.getX();
double yDiff = p2.getY() - p1.getY();
return Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
}
}
```
在main()函数中,创建一个三角形对象并计算周长和面积:
```java
public static void main(String[] args) {
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 5);
Point p3 = new Point(3, 0);
Triangle triangle = new Triangle();
triangle.setTri(p1, p2, p3);
double perimeter = triangle.getPerimeter();
double area = triangle.getArea();
System.out.println("三角形的周长为:" + perimeter);
System.out.println("三角形的面积为:" + area);
}
```
以上代码创建了一个三角形对象,给定了三个点的坐标,并计算出了三角形的周长和面积。
阅读全文