Calculating the geometric center of a 2D triangle

From CS100 Homework 1-2

题目描述

In this problem, you are required to write a C program in order to calculate the geometric center of any given 2D triangle, see the following figure:

image

Note that we do not require you to draw such an image; only necessary parameters for the geometric center are required to be output. Thus, you will input three 2D vertex coordinates, and then output the geometric center of this triangle as well as the maximum and minimum distance from this geometric center to the 3 given vertices.

Note that there are multiple methods to compute the center coordinates. The simplest method is,

image

wherex1, x2, x3are x coordinates of three inputs;y1, y2, y3are y coordinates of three inputs;

And the distance between two points can be calculated as

image

The following is a demo process for the program if you can do it successfully: (red indicates input)

image
问题输入

The input contains3 lines, for each line, it containstwo floating point numbers, indicating thex coordinateandy coordinatefor a vertex in the triangle.

* Note that we assume all inputs are valid! (It means you do not need to consider the illegal cases)

问题输出

1. output a prompt"Input vertex 1: ", then, receive two inputs.

2. output a prompt"Input vertex 2: ", then, receive two inputs.

3. output a prompt"Input vertex 3: ", then, receive two inputs.

4. output a prompt"The center of the triangle is: ", and followed by the values ofcenter_xandcenter_y

5. output a prompt"The maximum distance between center and vertices: ", and then the maximum distance between the center and the vertices.

6. output a prompt"The minimum distance between center and vertices: ", and then the minimum distance between the center and vertices.

* Center coordinates should be printed with “printf” to display only 1 decimal point digit, and distances should be printed with “printf” to display only 2 decimal point digit.

* The values of coordinates are between -10000.0 and 10000.0.

* (The quotation marks are not to be printed.)

样例输入1
0.0 1.0
-1.0 0.0
1.0 0.0

样例输出1
Input vertex 1: Input vertex 2: Input vertex 3: The center of the triangle is: 0.0 0.3
The maximum distance between center and vertices: 1.05
The minimum distance between center and vertices: 0.67
在线测试 (语言:C;时间限制:1s)
当前状态

无提交记录

有话想说?