如何确保我的对角线保持在矩形内部?

huangapple go评论64阅读模式
英文:

How do I ensure my diagonal lines stay contained within a rectangle?

问题

我试图绘制一些线条以对角线穿过矩形,但问题是线条超出了矩形边界,看看这张图片以了解我的意思:

如何确保我的对角线保持在矩形内部?

这是使用的代码:

@Override
public void paint(Graphics gr) {
    super.paint(gr);

    gr.setColor(Color.blue);
    int x = 100, y = 100, width = 100, height = 300;
    gr.fillRect(x, y, width, height);

    gr.setColor(Color.red);

    // 设置线条样式
    Graphics2D g2d = (Graphics2D) gr;
    g2d.setStroke(new BasicStroke(0.75f));

    float unitHeight = 50f; // 线间距
    int lines = (int) Math.ceil(height / unitHeight);
    int lineOffset = width / 5;

    for (int i = 1; i <= lines; ++i) {
        g2d.drawLine(x + lineOffset, y, x, y + lineOffset);
        g2d.drawLine(x + width - lineOffset, y + height, x + width, y + height - lineOffset);
        int cellWidth = width / 3;
        lineOffset += cellWidth;
    }
}

我正在使用Java Swing和AWT库来实现这个解决方案,但实际上我会在Android上使用它。

我尝试了一些解决方案,但未能完全解决问题。其中一种尝试是检查线条的起点坐标是否超出了矩形边界,但在那之后无法将点设置到正确的位置。我还尝试使用直线方程找到线条与矩形边界相交的点,但仍然找不到合适的解决方案。

所以基本上如何在矩形内绘制这些线条?另外,我没有选项来在矩形外绘制线条以使它们消失,以防有人认为可以这样做。

英文:

I'm trying to draw some lines to cross a rectangle diagonally but the issue is that the lines get out of the rectangle boundaries, look at this image to understand what I mean:

如何确保我的对角线保持在矩形内部?

here is the code used:

    @Override
    public void paint(Graphics gr) {
        super.paint(gr);

        gr.setColor(Color.blue);
        int x = 100, y = 100, width = 100, height = 300;
        gr.fillRect(x, y, width, height);

        gr.setColor(Color.red);

        // set stroke
        Graphics2D g2d = (Graphics2D) gr;
        g2d.setStroke(new BasicStroke(0.75f));

        float unitHeight = 50f; // line space
        int lines = (int) Math.ceil(height / unitHeight);
        int lineOffset = width / 5;

        for (int i = 1; i &lt;= lines; ++i) {
            g2d.drawLine(x + lineOffset, y, x, y + lineOffset);
            g2d.drawLine(x + width - lineOffset, y + height, x + width, y + height - lineOffset);
            int cellWidth = width / 3;
            lineOffset += cellWidth;
        }
    }

I'm using Java Swing and AWT libraries to implement the solution, however I would use this for Android actually.

I tried some solutions but I counldn't manage to fully solve the problem, one of these tries was to check if the line starting point coordinates are exceeding the rectangle boundaries but couldn't set the point to the right position after that. I also tried to find the point where the line intersects with the rectangle boundary using straight line equation but still couldn't find the appropriate solution.

so basicly how to draw the lines inside the rectangle? also I don't have the option to draw over the lines outside the the rectangle to make them disappear, just in case someone thinks to do so.

答案1

得分: 1

for 循环之前,您可以添加

g2d.setClip(x, y, width, height);

这样,您在该边界之外进行的任何绘图都将被省略并不显示在屏幕上,这意味着只有矩形内部的线条实际上会打印到屏幕上。当 for 循环完成时,您可以将剪切更改为整个屏幕,以允许添加任何其他您可能想要添加的绘图。

英文:

Before the for loop, you could add

g2d.setClip(x, y, width, height);

This makes any drawings you make outside of that boundary to be omitted and not be shown on the screen, which means that only the lines inside the rectangle will actually be printed out on the screen. When the for loop has finished, you could then change the clip to the whole screen to allow for any other drawings that you may want to add.

huangapple
  • 本文由 发表于 2023年5月25日 01:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定