题目:
编程实现绘制正N边形,且可以指定正N边形的边数,边长,中心坐标
public class RegularPolygon {
private int CenterX;private int CenterY;private int NumVerge;private double LenVerge;public int getCenterX() {return CenterX;}public void setCenterX(int centerX) {this.CenterX = centerX;}public int getCenterY() {return CenterY;}public void setCenterY(int centerY) {this.CenterY = centerY;}public int getNumVerge() {return NumVerge;}public void setNumVerge(int numVerge) {this.NumVerge = numVerge;}public double getLenVerge() {return LenVerge;}public void setLenVerge(int lenVerge) {this.LenVerge = lenVerge;}/*** 正N边形的面积计算公式:S=0.5sin(2π/N)NR^2* 其中R为外接圆半径,若边长为L* 则sin(π/N)=L/2R* 经过推导得S=NL^2cot(π/N)/4*/public double getArea() {
return this.NumVerge*Math.pow(LenVerge, 2)*Math.atan(Math.PI/(double)this.NumVerge)/4;}public double getPerimeter() {
return this.LenVerge*this.NumVerge;}}
import java.util.Scanner
import javafx.application.Application
import javafx.collections.ObservableList
import javafx.scene.Scene
import javafx.scene.layout.Pane
import javafx.scene.paint.Color
import javafx.scene.shape.Polygon
import javafx.stage.Stagepublic class Main extends Application{public static RegularPolygon input() {System.out.println("请输入正n边形边数和边长:")Scanner input=new Scanner(System.in)int n=input.nextInt()int len=input.nextInt()System.out.println("请输入正n边形中心坐标:")int centerx=input.nextInt()int centery=input.nextInt()RegularPolygon g=new RegularPolygon()g.setCenterX(centerx)g.setCenterY(centery)g.setLenVerge(len)g.setNumVerge(n)System.out.println("面积:"+g.getArea())System.out.println("周长:"+g.getPerimeter())return g}@Overridepublic void start(Stage primaryStage) {RegularPolygon g=input()Pane pane=new Pane()Polygon polygon=new Polygon()pane.getChildren().add(polygon)polygon.setFill(Color.PINK)ObservableList<Double> list= polygon.getPoints()double centerX=g.getCenterX(),centerY=g.getCenterY()double R=0.5*g.getLenVerge()/Math.sin(Math.PI/g.getNumVerge())for(int i=0list.add(centerX+R*Math.cos(Math.PI/2+i*2*Math.PI/g.getNumVerge()))list.add(centerY+R*Math.sin(Math.PI/2+i*2*Math.PI/g.getNumVerge()))}Scene scene=new Scene(pane,600,600)primaryStage.setTitle("正n边形")primaryStage.setScene(scene)primaryStage.show()}public static void main(String [] args) {Application.launch(args)}
}