简体   繁体   English

绘制任意形状的形状

[英]Drawing Arbitrarily Sided Shape

I'm writing a drawing program and one feature I would like to implement is arbitrarily sided shapes. 我正在编写一个绘图程序,我想要实现的一个功能是任意的双面形状。 I have most of the features worked out, but one thing I need is a function to generate a Polygon object from an integer representing a number of sides. 我已经完成了大部分功能,但我需要的一个功能是从表示多个边的整数生成Polygon对象。 I don't remember much of trigonometry, although I'm sure that my problem involves some. 我不记得三角学,虽然我确信我的问题涉及一些。

There are two parts to your problem. 你的问题分为两部分。 First, you need an algorithm for generating points that comprise vertices of a polygon, which is a language-agnostic process. 首先,您需要一种算法来生成包含多边形顶点的点,这是一个与语言无关的过程。 Based on the wording of your question, it seems like any polygon with the required number of sides works, so you could generate a regular polygon based on a circle of fixed radius. 根据您问题的措辞,似乎任何具有所需边数的多边形都可以工作,因此您可以基于固定半径的圆生成正多边形。

For example, for the input 4 , your points might be (0, r) , (r, 0) , (0, -r) and (-r, 0) . 例如,对于输入4 ,您的点可能是(0, r)(r, 0)(0, -r)(-r, 0) You'd get those by drawing an imaginary/invisible circle of radius r , then selecting points (sin(360/input)*r, cos(360/input)*r) . 你可以通过绘制一个半径为r的假想/不可见圆圈然后选择点(sin(360/input)*r, cos(360/input)*r) (Keep in mind that Java's trig uses radians, not degrees, though.) (请记住,Java的trig使用弧度,而不是度数。)

Once you have your points, you have to create the Polygon object. 获得分数后,必须创建Polygon对象。 There's a constructor that takes an array of x-coordinates and an array of y-coordinates, plus the total number of vertices, which is just your initial input. 有一个构造函数 ,它接受一个x坐标数组和一个y坐标数组,加上顶点总数,这只是你的初始输入。 All you really have to do is pop the coordinates of your points into two arrays and you're all set. 你真正需要做的就是将你的点的坐标弹出两个数组,然后你就完成了。

Hope this helps. 希望这可以帮助。 It provides code for regular polygons. 它提供常规多边形的代码。 http://java-sl.com/shapes.html http://java-sl.com/shapes.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM