如何使用点在java中绘制一条线?

时间:2016-02-03 10:16:38

标签: java arraylist

我想知道如何在Java中使用不同的点来画一条线。这是我到目前为止的代码。我宁愿不得到每个点的x和y,只是让程序自己使用x,y中的点。我仍然是编程的新手,并且在这一点上不太擅长使用javadoc。以下是我现在的代码:

 //Create a Polygon class. A polygon is a closed shape with lines joining the corner points. 
 //You will keep the points in an array list. Use object of java.awt.Point for the point.

 //Polygon will have as an instance variable an ArrayList of Points to hold the points
 //The constructor takes no parameters but initializes the instance variable.  The
 //Polygon class also has the following methods:
 //    add: adds a Point to the polygon
 //    perimeter: returns the perimeter of the polygon
 //    draw: draws the polygon by connecting consecutive points and then
 //          connecting the last point to the first.
 //
 //No methods headers or javadoc is provided this time. You get to try your hand at writing
 //a class almost from scratch

 // Need help starting this question? In the lesson titled
 // "Starting points: Problem Set Questions", go to the
 // problem titled "Problem Set 6 - Question 3" for some tips on 
 // how to begin.

 import java.util.ArrayList; import java.awt.Point;

 public class Polygon {
     ArrayList<Point> points;

     /**
     **  a Polygon represents a shape with flat lines and several points.
     */ 
     public Polygon(){
     /**
     * Constructs empty array of points.
     */
     points = new ArrayList<Point>();
     }    /**    ** Adds points to the polygon points Array.    *@param = points x and y coordinates to add to class    */
     public void addPoints(Point point){
         points.add(point);
     }
     /**
     **gets distance between points in ArrayList
     */
     public double perimeter(){
         double perimeter = 0;
         int i = 0;
         while(i<points.size()){
             if(i<points.size()-1){
              perimeter = perimeter + points.get(i).distance(points.get(i+1));   
             }
             else{
              perimeter = perimeter + points.get(i).distance(points.get(0));   
             }
         }
         return perimeter;
     }
      /**    * Draws Polygon using points from points ArrayList    */
     public void drawPolygon(){
      int i = 0;
         while(i < points.size()-1){
           Line2D line = Line2D.Float(points.get(i), points.get(i+1));
             line.draw();
         }
         Line2D lastLine = Line2D.Float(points.get(0), points.get(points.size()-1));
         lastLine.draw();
     } }

2 个答案:

答案 0 :(得分:0)

让我理解这一点,你想实现draw()方法吗?

如果是这样,你需要做的是搜索 Bresenham算法,它根据两个给定的点绘制一条线。

答案 1 :(得分:0)

我知道这个确切的问题,因为我也遇到了一些主要问题。我在 Udacity 的 Java 基础课程中偶然发现了这个问题。

我被困住了,直到我进入后续课程,这些课程继续解释有关课程的更多信息。我从 https://www.udacity.com/wiki/cs046/code 下载了问题集 6 zip 文件并导航到 Polygon Project 文件夹。在那里,我打开了 BlueJay 项目文件,可以看到可用于我的 Polygon 类的所有其他类。

在我的 Polygon draw 方法中初始化 Line 后,Polygon 类和 Line 类之间的连接出现了(万岁)。

您可以打开 Line 类以查看其构造函数和方法是什么。显然 Line 类正在与其他几个类一起工作以实际能够绘制线条,但我们在 Udacity IDE 中看不到任何这些,问题也没有真正告诉我们..

尽管如此,我还是添加了在 Udacity IDE 中运行以获得正确输出的代码。

我看到您在 Point 类上使用了距离方法。我不认为这存在.. 因此在我的代码中间是垃圾。我可能可以以某种方式浓缩它,但我很高兴得到正确的输出。

#include <iostream>
#include <map>

using namespace std;
map<int, int> memo;

int stepPerms(int n) {
    if (memo.find(n) != memo.end())
        return memo[n];
    if (n == 0)
        return 0;
    if (n == 1)
        return 1;
    if (n == 2)
        return 2;
    if (n == 3)
        return 4;
    for (int i = 1; i <= 3; i++)
        memo[n] += stepPerms(n - i);
    return memo[n];
}

int main() {
    cout << stepPerms(27);
}