第四章第七题(顶点坐标)(Corner point coordinates)

第四章第七题(顶点坐标)(Corner point coordinates)

  • *4.7(顶点坐标)假设一个正五边形的中心位于(0,0),其中一个点位于0点位置。编写一个程序,提示用户输入正五边形外接圆的半径,以p1到p5的顺序显示正五边形上五个顶点的坐标。使用控制台格式来显示小数点后两位数字。
    这里是一个运行示例:
    Enter the radius of the bounding circle: 100.52
    The coordinates of five points on the pentagon are
    (95.60, 31.06)
    (0.00, 100.52)
    (-95.60, 31.06)
    (-59.08, -81.32)
    (59.08, -81.32)

    *4.7(Corner point coordinates) Suppose a pentagon is centered at (0, 0) with one point at the 0 o’clock position, as shown in Figure 4.4c. Write a program that prompts the user to enter the radius of the bounding circle of a pentagon and displays the coordinates of the five corner points on the pentagon from p1 to p5 in this order.Use console format to display two digits after the decimal point.
    Here is a sample run:
    Enter the radius of the bounding circle: 100.52
    The coordinates of five points on the pentagon are
    (95.60, 31.06)
    (0.00, 100.52)
    (-95.60, 31.06)
    (-59.08, -81.32)
    (59.08, -81.32)

  • 参考代码:

package chapter04;

import java.util.Scanner;

public class Code_07 {
    public static void main(String[] args) {
        double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, radius;

        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Enter the radius of the bounding circle: ");
        radius = inputScanner.nextDouble();

        x1 = radius * Math.cos(Math.PI / 2 - (2 * Math.PI / 5));
        y1 = radius * Math.sin(Math.PI / 2 - (2 * Math.PI / 5));

        x2 = radius * Math.cos(Math.PI / 2);
        y2 = radius * Math.sin(Math.PI / 2);

        x3 = radius * Math.cos(Math.PI / 2 + (2 * Math.PI / 5));
        y3 = radius * Math.sin(Math.PI / 2 + (2 * Math.PI / 5));

        x4 = radius * Math.cos(Math.PI / 2 + 2 * (2 * Math.PI / 5));
        y4 = radius * Math.sin(Math.PI / 2 + 2 * (2 * Math.PI / 5));

        x5 = radius * Math.cos(Math.PI / 2 - 2 * (2 * Math.PI / 5));
        y5 = radius * Math.sin(Math.PI / 2 - 2 * (2 * Math.PI / 5));

        System.out.println("The coordinates of five points on the pentagon are");
        System.out.printf("(%.2f, %.2f)\n", x1, y1);
        System.out.printf("(%.2f, %.2f)\n", x2, y2);
        System.out.printf("(%.2f, %.2f)\n", x3, y3);
        System.out.printf("(%.2f, %.2f)\n", x4, y4);
        System.out.printf("(%.2f, %.2f)\n", x5, y5);

        inputScanner.close();
    }
}

  • 结果显示:
Enter the radius of the bounding circle: 100.52
The coordinates of five points on the pentagon are
(95.60, 31.06)
(0.00, 100.52)
(-95.60, 31.06)
(-59.08, -81.32)
(59.08, -81.32)

Process finished with exit code 0

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值