繁体   English   中英

缩放和缩放后屏幕坐标和图像坐标之间的转换

[英]Translations between screen co-ordinates and image co-ordinates after scaling and zooming

我有一个课程,负责管理从屏幕坐标到图像坐标的转换。
但是,我有一个“一个错误”。

以下给出318,198,而不是319,199:

@Test
public void test6rightScreenCornerToImageCoOrdAfterZoomingAndScaling() {
    PointTranslation pointTranslation = new PointTranslation();
    pointTranslation.setOriginalSize(0, 0, 320, 200); // original image
    pointTranslation.zoomIn(9, 9, 310, 190); // zoomed image starting at 9,9
    pointTranslation.scale(0, 0, 800, 800);

    Point translatedPoint = pointTranslation.transformPoint(799,799);
    System.out.println(testName.getMethodName() + " : " + translatedPoint.toString());
    assertTrue(translatedPoint.x == 319);
    assertTrue(translatedPoint.y == 199);
}

================================================== ===========

完整清单:

package gtx;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.gdip.PointF;
import org.eclipse.swt.internal.gdip.RectF;

@SuppressWarnings("restriction")
public class PointTranslation {
RectF originalSize = null;
RectF currentSize = null;
RectF scaledSize = null;

public void setOriginalSize(int originX, int originY, int width, int height) {
    originalSize = getRectangle(originX, originY, width, height);
    currentSize = originalSize;
}

public void zoomIn(int originX, int originY, int width, int height) {
    // System.out.println("addTranslation: " + originX + " " + originY + " "
    // + width + " " + height);
    currentSize = getRectangle(originX, originY, width, height);
}

public void scale(int originX, int originY, int width, int height) {
    // System.out.println("addTranslation: " + originX + " " + originY + " "
    // + width + " " + height);
    scaledSize = getRectangle(originX, originY, width, height);
}

public boolean isPointWithinBounds(Point point) {
    return isPointWithinBounds(point.x, point.y);
}

public boolean isPointWithinBounds(int xPos, int yPos) {
    boolean ret = false;

    if (scaledSize != null) {
        RectF sourceRec = scaledSize;
        int xBounds = (int) (sourceRec.Width + sourceRec.X);
        int yBounds = (int) (sourceRec.Height + sourceRec.Y);
        ret = (xPos < xBounds) && (yPos < yBounds) && (xPos > sourceRec.X) && (yPos > sourceRec.Y);
    }

    return ret;
}

public Point transformPoint(Point point) {
    return transformPoint(point.x, point.y);
}

public Point transformPoint(int xPos, int yPos) {
    Point sourcePoint = new Point((int) xPos, (int) yPos);
    Point retPoint = sourcePoint;

    if (this.scaledSize != null) {
        retPoint = transformPoint(this.scaledSize, this.currentSize, sourcePoint);
    }
    return retPoint;
}

/*
 * Rectangle 1 has (x1, y1) origin and (w1, h1) for width and height, and
 * Rectangle 2 has (x2, y2) origin and (w2, h2) for width and height, then
 * 
 * Given point (x, y) in terms of Rectangle 1 co-ords, to convert it to
 * Rectangle 2 co-ords: xNew = ((x-x1)/w1)*w2 + x2; yNew = ((y-y1)/h1)*h2 +
 * y2;
 */
private Point transformPoint(RectF source, RectF destination, Point intPoint) {
    PointF point = new PointF();
    point.X = intPoint.x;
    point.Y = intPoint.y;
    return transformPoint(source, destination, point);
}

private Point transformPoint(RectF source, RectF destination, PointF point) {
    return new Point((int) ((((point.X - source.X) / source.Width) * destination.Width + destination.X)),
            (int) ((((point.Y - source.Y) / source.Height) * destination.Height + destination.Y)));
}

private RectF getRectangle(int x, int y, int width, int height) {
    RectF rect = new RectF();
    rect.X = x;
    rect.Y = y;
    rect.Height = height;
    rect.Width = width;
    return rect;
}

private PointF getPoint(int x, int y) {
    PointF retPoint = new PointF();
    retPoint.X = x;
    retPoint.Y = y;
    return retPoint;
}

public void reset() {
    this.originalSize = null;
    this.currentSize = null;
    this.scaledSize = null;
}
}

更新:

我的问题似乎肯定是四舍五入。 奇怪的是,对于一些测试用例我需要Round Up来获得正确的Point,有时我需要向上舍入。 我遗漏了像缩放因子之类的东西。 任何建议如何正确地在两个矩形之间进行翻译?

更新2:

我尝试了以下方法,但仍然没有快乐:

    private Point transformPoint(RectF source, RectF destination, PointF point) {   
        float xPercent = normalize(point.X,source.X,source.Width);
        float destX = xPercent*(Math.abs(destination.Width - destination.X)) + destination.X;

        float yPercent = normalize(point.Y,source.Y,source.Height);
        float destY = yPercent*(Math.abs(destination.Height - destination.Y)) + destination.Y;

        System.out.println("Float x,y: " + destX + ", " + destY);
        System.out.println("Ceil Float x,y: " + Math.floor(destX) + ", " + Math.floor(destY) );

        return new Point((int)Math.floor(destX), (int)Math.floor(destY));
    }

    private float normalize(float value, float min, float max) {
        return Math.abs((value - min) / (max - min));
    }

在运行测试用例并单步执行代码时,我的调试显示以下替换...

transformPoint(RectF source, RectF destination, PointF point) {
    return new Point (
        (int) (((( 799 - 0 ) / 800 ) * 310 ) + 9 )),
        (int) (((( 799 - 0 ) / 800 ) * 190 ) + 9 ))
    );
}

等式的前半部分返回318.6125 等式的198.7625返回198.7625

你需要

  1. 修改方程,以便int转换截断到所需的值(例如结尾处的+ 1
  2. 在转换为int之前向上舍入
  3. 和结果一起生活

正如Mathew所指出的那样,连续多次翻译会扭曲和放大问题,就像平均值一样。

如Reenactor所述,您需要在转换为int之前对点进行舍入:

private Point transformPoint(RectF source, RectF destination, PointF point) {
   final int ptx = Math.round((((point.X - source.X) / source.Width) * destination.Width + destination.X));
   final int pty = Math.round((((point.Y - source.Y) / source.Height) * destination.Height + destination.Y));
   return new Point(ptx, pty);    
}

您是否有可能在每次转换后将中间结果转换为整数点?

请记住, org.eclipse.swt.graphics.Pointxy存储为int ,因此计算中的任何分数都将被删除

那是:

1)第一次翻译:

x1 = ((799 - 0) / 800) * 301 + 9 = 309.62375
y1 = ((799 - 0) / 800) * 181 + 9 = 189.77375

如果您在进入下一个翻译之前将它们存储在Point对象中,它们将被截断为(309,189)并且您将获得

2)第二次翻译

x2 = ((309 - 9) / 301) * 320 + 0 = 318.93...
y2 = ((189 - 9) / 181) * 200 + 0 = 198.89...

反过来,这将被截断为(318,198)。

暂无
暂无

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

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