遍历java中一行/路径上的每个点

iterate through each point on a line/path in java(遍历java中一行/路径上的每个点)
本文介绍了遍历java中一行/路径上的每个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我是使用迭代器的新手,想知道如何遍历线段上的每个点(准确地说是 Line2D.Double)——我需要检查线上的每个点是否满足某些要求.

I'm new to using iterators and was wondering how one would iterate through each point on a line segment (Line2D.Double, to be precise) -- I need to check to see if each point on the line fulfills certain requirements.

另外,给定一个路径对象(如 GeneralPath),你会如何做同样的事情(遍历形状轮廓上的每个点)?

Also, given a path object (like GeneralPath), how would you do the same thing (iterate through each point on the outline of the shape)?

理想情况下,我想要这样的东西(带有一条线或一条路径):

Ideally I'd like something like this (with either a line or a path):

Line2D line = new Line2D.Double(p1,p2);
for (Point2D point : line)
{
   point.callSomeMethod();
}

推荐答案

Java API 中似乎没有任何东西可以让 Bresenham 的算法对用户可见.所以我写了一个遍历一行的类.

There seems to be nothing in the Java API that makes Bresenham's algorithm user-visible. So I wrote a class that iterates over a line.

你可以这样使用它:

List<Point2D> points = new ArrayList<Point2D>();
Line2D line = new Line2D.Double(0, 0, 8, 4);
Point2D current;

for (Iterator<Point2D> it = new LineIterator(line); it.hasNext();) {
    current = it.next();
    points.add(current);
}

assertThat(points.toString(), 
    is("[Point2D.Double[0.0, 0.0], Point2D.Double[1.0, 0.0], " +
        "Point2D.Double[2.0, 1.0], Point2D.Double[3.0, 1.0], " +
        "Point2D.Double[4.0, 2.0], Point2D.Double[5.0, 2.0], " +
        "Point2D.Double[6.0, 3.0], Point2D.Double[7.0, 3.0], " +
        "Point2D.Double[8.0, 4.0]]"));

这篇关于遍历java中一行/路径上的每个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)