在2D坐标上流/ IntRange?

时间:2017-07-23 23:09:41

标签: java coordinates java-stream

我有以下课程:

public class Rectangle
{
   public int width, height;
   public Point start;

   public Rectangle(Point start, int width, int height)
   {
      this.start = start;
      this.width = width;
      this.height = height;
   }
}

public class Point
{
   public int x, y;

   public Point(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
}

我想创建一个Rectangle类的方法,以便返回类型为Stream的Java 8 Point,它将在矩形内部流式传输索引位置。防爆。

// This is inside the rectangle class.
public Stream<Point> stream(int hstep, int vstep)
{
   // if the rectangle started at 0,0
   // and had a width and height of 100
   // and hstep and vstep were both 1
   // then the returned stream would be
   // 0,0 -> 1,0 -> 2,0 -> ... -> 100,0 -> 0,1 -> 1,1 -> ...

   // If vstep was 5 and h step was 25 it would return
   // 0,0 -> 25,0 -> 50,0 -> 75,0 -> 100,0 -> 0,5 -> 25,5 -> ...

   return ...
}

我经常使用IntStreammapfilter等,但这比我曾经尝试过的任何东西都复杂得多。我不知道我会怎么做那样的事情。有人能引导我指向正确的方向吗?

4 个答案:

答案 0 :(得分:2)

您可以使用嵌套的IntStream生成每个Point,然后展平生成的流:

public Stream<Point> stream(int hstep, int vstep) {
    return IntStream.range(0, height / vstep)
            .mapToObj(y -> IntStream.range(0, width / hstep)
                    .mapToObj(x -> new Point(start.x + x * hstep, start.y + y * vstep)))
            .flatMap(Function.identity());
}

答案 1 :(得分:1)

您可以使用iterate(...)limit(...)生成xy。 然后,您将两个流合并,并使用PointflatMap(...)

创建map(...)
public Stream<Point> stream(int hstep, int vstep) {
    return Stream.iterate(this.start.x, s-> s + hstep).
            limit(this.width / hstep).
            flatMap(x-> Stream.iterate(this.start.y, s-> s + vstep).
                    limit(this.height / vstep).map(y-> new Point(x, y)));
}

答案 2 :(得分:0)

这是实现目标的一种方式。

import java.util.function.Supplier;
import java.util.stream.Stream;

public class Rectangle
{
    public int width, height;
    public Point start;

    public Rectangle(Point start, int width, int height)
    {
        this.start = start;
        this.width = width;
        this.height = height;
    }

    public Stream<Point> stream(int hstep, int vstep)
    {
        int hSteps = 1 + ((width - 1) / hstep);
        int vSteps = 1 + ((height - 1) / vstep);
        int numPoints = hSteps * vSteps;

        return Stream.generate(new Supplier<Point>()
        { 
            int origStartX = start.x;
            int origWidth = width;

            int x = start.x;
            int y = start.y;

            @Override
            public Point get()
            {
                Point pt = new Point(x, y);

                x += hstep;
                if (x >= origWidth)
                {
                    x = origStartX;
                    y += vstep;
                }

                return pt;
            }
        }).limit(numPoints);
    }

    public static void main(String[] args)
    {
        Rectangle r = new Rectangle(new Point(0, 0), 100, 100);

        Stream<Point> s = r.stream(25, 5);
        s.forEach(System.out::println);
    }
}

public class Point
{
  public int x, y;

  public Point(int x, int y)
  {
     this.x = x;
     this.y = y;
  }

  public String toString()
  {
     return String.format("(%d, %d)", x, y);
  }
}

输出:

(0, 0)
(25, 0)
(50, 0)
(75, 0)
(0, 5)
(25, 5)
(50, 5)
(75, 5)
.
.
<snip>
.
.
(0, 90)
(25, 90)
(50, 90)
(75, 90)
(0, 95)
(25, 95)
(50, 95)
(75, 95)

答案 3 :(得分:0)

尝试获取一种类型的流并将其转换为点流。这应该让你开始:

return IntStream.range(0, width * height)
    .mapToObj(i -> new Point(i % width, i / width));

它无法处理hstepvstep。看看你是否可以弄清楚如何将这些添加到混合中。