📜  计算机图形学中的视口转换窗口及其实现

📅  最后修改于: 2021-04-22 03:25:26             🧑  作者: Mango

视口转换窗口是将2D世界坐标对象转换为设备坐标的过程。世界或剪贴窗口中的对象被映射到视口,该视口是屏幕上要显示世界坐标的区域。

一般条款

  • 世界坐标–我们定义图表的是笛卡尔坐标wrt,例如X wmin ,X wmax ,Y wmin ,Y wmax
  • 设备坐标–这是要显示对象的屏幕坐标,例如X vmin ,X vmax ,Y vmin ,Y vmax
  • 窗口–这是世界坐标上选择显示的区域。
  • ViewPort –这是设备坐标上要显示图形的区域。

视口窗口的数学计算:

视口的大小可能会小于或大于窗口。在这些情况下,我们必须根据视口增加或减小Window的大小,为此,我们需要一些数学计算。

(xw, yw): A point on Window
(xv, yv): Corresponding  point on Viewport
  • 我们必须计算点(x v ,y v )

  • 现在,对象在“窗口”和“视口”中的相对位置相同。
    For x coordinate,                        
    

    For y coordinate,
    

  • 因此,在计算了x和y坐标之后,我们得到

  • 其中,s x是x坐标的比例因子,而y是y坐标的比例因子

例子:

假设

  • 对于窗口, X wmin = 20,X wmax = 80,Y wmin = 40,Y wmax = 80
  • 对于视口, X vmin = 30,X vmax = 60,Y vmin = 40,Y vmax = 60
  • 现在,窗口上的点(X w ,Y w )是(30,80)。我们必须在视口上计算该点
    (X v ,Y v )
  • 首先,使用上述公式计算x坐标S x的比例因子和y坐标S y的比例因子。
    Sx = ( 60 - 30 ) / ( 80 - 20 ) = 30 / 60
    Sy = ( 60 - 40 ) / ( 80 - 40 ) = 20 / 40
  • 因此,现在计算视口上的点(X v ,Y v )。
    Xv = 30 + ( 30 - 20 ) * ( 30 / 60 ) = 35
    Yv = 40 + ( 80 - 40 ) * ( 20 / 40 ) = 60
  • 因此,窗口上的点(X w ,Y w )=(30,80)将是视口上的(X v ,Y v )=(35,60)

这是上述方法的实现:

执行:

C++
// C program to implement
// Window to ViewPort Transformation
  
#include 
  
// Function for window to viewport transformation
void WindowtoViewport(int x_w, int y_w, int x_wmax,
                      int y_wmax, int x_wmin, int y_wmin,
                      int x_vmax, int y_vmax, int x_vmin,
                      int y_vmin)
{
    // point on viewport
    int x_v, y_v;
  
    // scaling factors for x coordinate and y coordinate
    float sx, sy;
  
    // calculatng Sx and Sy
    sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
  
    // calculating the point on viewport
    x_v = x_vmin + (float)((x_w - x_wmin) * sx);
    y_v = y_vmin + (float)((y_w - y_wmin) * sy);
  
    printf("The point on viewport: (%d, %d )\n ", x_v, y_v);
}
  
// Driver Code
void main()
{
    // boundary values for window
    int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
  
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
  
    // point on window
    int x_w = 30, y_w = 80;
  
    WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}


Java
// Java program to implement
// Window to ViewPort Transformation
class GFG
{
  
// Function for window to viewport transformation
static void WindowtoViewport(int x_w, int y_w, int x_wmax,
                    int y_wmax, int x_wmin, int y_wmin,
                    int x_vmax, int y_vmax, int x_vmin,
                    int y_vmin)
{
    // point on viewport
    int x_v, y_v;
  
    // scaling factors for x coordinate and y coordinate
    float sx, sy;
  
    // calculatng Sx and Sy
    sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
  
    // calculating the point on viewport
    x_v = (int) (x_vmin + (float)((x_w - x_wmin) * sx));
    y_v = (int) (y_vmin + (float)((y_w - y_wmin) * sy));
  
    System.out.printf("The point on viewport: (%d, %d )\n ", x_v, y_v);
}
  
// Driver Code
public static void main(String[] args)
{
  
    // boundary values for window
    int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
  
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
  
    // point on window
    int x_w = 30, y_w = 80;
  
    WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}
}
  
// This code is contributed by Rajput-Ji


Python 3
# Python3 program to implement
# Window to ViewPort Transformation
  
# Function for window to viewport transformation
def WindowtoViewport(x_w, y_w, x_wmax, y_wmax,
                       x_wmin, y_wmin, x_vmax,
                       y_vmax, x_vmin, y_vmin):
                             
    # point on viewport
    # calculatng Sx and Sy
    sx = (x_vmax - x_vmin) / (x_wmax - x_wmin)
    sy = (y_vmax - y_vmin) / (y_wmax - y_wmin)
  
    # calculating the point on viewport
    x_v = x_vmin + ((x_w - x_wmin) * sx)
    y_v = y_vmin + ((y_w - y_wmin) * sy)
  
    print("The point on viewport:(", int(x_v), 
                                ",", int(y_v), ")")
  
# Driver Code
if __name__ == '__main__':
      
    # boundary values for window
    x_wmax = 80
    y_wmax = 80
    x_wmin = 20
    y_wmin = 40
  
    # boundary values for viewport
    x_vmax = 60
    y_vmax = 60
    x_vmin = 30
    y_vmin = 40
  
    # point on window
    x_w = 30
    y_w = 80
  
    WindowtoViewport(30, 80, 80, 80, 20,
                     40, 60, 60, 30, 40)
      
# This code is contributed by Surendra_Gangwar


C#
// C# program to implement
// Window to ViewPort Transformation
using System;
  
class GFG
{
  
// Function for window to viewport transformation
static void WindowtoViewport(int x_w, int y_w, 
                             int x_wmax, int y_wmax, 
                             int x_wmin, int y_wmin,
                             int x_vmax, int y_vmax, 
                             int x_vmin, int y_vmin)
{
    // point on viewport
    int x_v, y_v;
  
    // scaling factors for x coordinate 
    // and y coordinate
    float sx, sy;
  
    // calculatng Sx and Sy
    sx = (float)(x_vmax - x_vmin) / 
                (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) / 
                (y_wmax - y_wmin);
  
    // calculating the point on viewport
    x_v = (int) (x_vmin + 
        (float)((x_w - x_wmin) * sx));
    y_v = (int) (y_vmin + 
        (float)((y_w - y_wmin) * sy));
  
    Console.Write("The point on viewport: " +
                 "({0}, {1} )\n ", x_v, y_v);
}
  
// Driver Code
public static void Main(String[] args)
{
  
    // boundary values for window
    int x_wmax = 80, y_wmax = 80, 
        x_wmin = 20, y_wmin = 40;
  
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60, 
        x_vmin = 30, y_vmin = 40;
  
    // point on window
    int x_w = 30, y_w = 80;
  
    WindowtoViewport(30, 80, 80, 80, 20, 
                     40, 60, 60, 30, 40);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
The point on viewport: (35, 60 )