如何指定立方体的Z方向(深度)

时间:2019-03-05 09:36:46

标签: java 3d

我想知道如何为立方体指定z方向。需要沿z方向将其转换为-5。 整个问题陈述是这样的:通过在3d空间中定义一个多维数据集,在坐标系的原点设置一个摄影机以及一个具有90°FOV的投影矩阵,来实现转换管道。多维数据集大小应为2个单位(2x2x2),沿负z轴放置5个单位     包转换;

import java.util.*;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class Transformations {

        //declare list of polygons
        static ArrayList<Integer>p_left=new ArrayList<Integer>();
        static ArrayList<Integer>p_right=new ArrayList<Integer>();
        static ArrayList<Integer>p_down=new ArrayList<Integer>();
        static ArrayList<Integer>p_top=new ArrayList<Integer>();
        static ArrayList<Integer>p_near=new ArrayList<Integer>();
        static ArrayList<Integer>p_far=new ArrayList<Integer>();


        static int count=0;
        public static BufferedImage gradientSetRaster(BufferedImage img) {
            int width = img.getWidth();
            int height = img.getHeight();

            WritableRaster raster = img.getRaster();
            int[] pixel = new int[3]; //RGB

                for (int y = 0; y < height; y++) {
                    int val = (int) (y * 255f / height);
                    for (int shift = 1; shift < 3; shift++) {
                        pixel[shift] = val;
                    }

                    for (int x = 0; x < width; x++) {
                        raster.setPixel(x, y, pixel);
                    }
                }

            return img;
        }

        public static BufferedImage drawLine(BufferedImage img, int x0, int y0, int xn, int yn) 
        {
            WritableRaster raster=img.getRaster();

            int[] pixel = new int[3]; //RGB
            for(int k=0; k<3; k++) 
            {
                pixel[k]=255;
            }

            int dx=Math.abs(xn-x0);
            int dy=Math.abs(yn-y0);
            int m=dy/dx;
            int twoDy=2*dy;
            int twoDyMintwoDx=twoDy-2*dx;

            if(dx>dy)   //slope is positive and is less than or equal to 1 
            {
                int p=twoDy-dx;

                for(int k=0; k<dx-1; k++) 
                {
                    if(p<0) 
                    {
                        x0=x0+1;
                        raster.setPixel(x0, y0, pixel);
                        p=p+twoDy;
                    }
                    else 
                    {
                        x0=x0+1;
                        y0=y0+1;
                        raster.setPixel(x0, y0, pixel);
                        p=p+twoDy-2*dx;
                    }

                }
            }

            if(dy>dx) 
            {
                int p=2*dx-dy;
                int temp1=x0;
                x0=y0;
                y0=temp1;

                int temp2=xn;
                xn=yn;
                yn=temp2;

                for(int k=0; k<dx-1; k++) 
                {
                    x0=x0+1;
                    y0=y0+1;
                    raster.setPixel(x0, y0, pixel);
                }
            }

            if(dy==dx) 
            {
                x0=x0+1;
                y0=y0+1;

                raster.setPixel(x0, y0, pixel);
            }


            return img;
        }

        public static BufferedImage drawUpPolygon(Graphics g, BufferedImage img, ArrayList<Integer>p_top) 
        {
            g.drawLine(p_top.get(0), p_top.get(1), p_top.get(2), p_top.get(3));
            g.drawLine(p_top.get(0), p_top.get(1), p_top.get(4), p_top.get(5));
            g.drawLine(p_top.get(4), p_top.get(5), p_top.get(6), p_top.get(7));
            g.drawLine(p_top.get(6), p_top.get(7), p_top.get(2), p_top.get(3));

            return img;
        }

        public static BufferedImage drawDownPolygon(Graphics g, BufferedImage img, ArrayList<Integer>p_top) 
        {
            g.drawLine(p_down.get(0), p_down.get(1), p_down.get(2), p_down.get(3));
            g.drawLine(p_down.get(0), p_down.get(1), p_down.get(4), p_down.get(5));
            g.drawLine(p_down.get(4), p_down.get(5), p_down.get(6), p_down.get(7));
            g.drawLine(p_down.get(6), p_down.get(7), p_down.get(2), p_down.get(3));

            return img;
        }

        public static BufferedImage drawBetween(Graphics g, BufferedImage img, ArrayList<Integer>p_top, ArrayList<Integer>p_down) 
        {
            g.drawLine(p_top.get(0), p_top.get(1), p_down.get(0), p_down.get(1));
            g.drawLine(p_top.get(2), p_top.get(3), p_down.get(2), p_down.get(3));
            g.drawLine(p_top.get(4), p_top.get(5), p_down.get(4), p_down.get(5));
            g.drawLine(p_top.get(6), p_top.get(7), p_down.get(6), p_down.get(7));

            return img;
        }



        public static void main(String... args) {
            Frame w = new Frame("Raster");  //window
            final int imageWidth = 500;
            final int imageHeight = 500;

            w.setSize(imageWidth,imageHeight);
            w.setLocation(100,100);
            w.setVisible(true);

            Graphics g = w.getGraphics();

            BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
            gradientSetRaster(img);
            g.drawImage(img, 0, 0, (img1, infoflags, x, y, width, height) -> false);  //draw the image. You can think of this as the display method.


            w.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    //order will be(vertex->index)
                    //lowerLeft->0,1
                    //upperLeft->2,3
                    //lowerRight->4,5
                    //upperRight->6,7
                    count++;

                    int t_lowLeftX=e.getX();
                    int t_lowLeftY=e.getY();
                    //top polygon                       
                    int t_lowRightX;
                    int t_lowRightY;

                    int t_uppLeftX;
                    int t_uppLeftY;

                    int t_uppRightX;
                    int t_uppRightY;

                    //bottom polygon
                    int d_lowLeftX;
                    int d_lowLeftY;

                    int d_lowRightX;
                    int d_lowRightY;

                    int d_uppLeftX;
                    int d_uppLeftY;

                    int d_uppRightX;
                    int d_uppRightY;

                    if(count==1) {
                        //lowerLeft indexes
                        t_lowLeftX=e.getX();
                        t_lowLeftY=e.getY();

                        t_lowRightX=t_lowLeftX+100;
                        t_lowRightY=t_lowLeftY;

                        t_uppLeftX=t_lowLeftX+50;
                        t_uppLeftY=t_lowLeftY+50;

                        t_uppRightX=t_uppLeftX+100;
                        t_uppRightY=t_uppLeftY;

                        d_lowLeftX=t_lowLeftX;
                        d_lowLeftY=t_lowLeftY-70;

                        d_lowRightX=t_lowRightX;
                        d_lowRightY=t_lowRightY-70;

                        d_uppLeftX=t_uppLeftX;
                        d_uppLeftY=t_uppLeftY-70;

                        d_uppRightX=t_uppRightX;
                        d_uppRightY=t_uppRightY-70;


                        //connect(lowLeft->uppLeft, lowLeft->lowRight, uppLeft->uppRight, 
                        //uppRight->lowRight ...)   
                        p_top.add(t_lowLeftX);
                        p_top.add(t_lowLeftY); 
                        p_top.add(t_lowRightX);
                        p_top.add(t_lowRightY);
                        p_top.add(t_uppLeftX);
                        p_top.add(t_uppLeftY);
                        p_top.add(t_uppRightX);
                        p_top.add(t_uppRightY);

                        p_down.add(d_lowLeftX);
                        p_down.add(d_lowLeftY);
                        p_down.add(d_lowRightX);
                        p_down.add(d_lowRightY);
                        p_down.add(d_uppLeftX);
                        p_down.add(d_uppLeftY);
                        p_down.add(d_uppRightX);
                        p_down.add(d_uppRightY);



//                      g.drawLine(lowLeftX, lowLeftY, uppLeftX, uppLeftY);
//                      g.drawLine(lowLeftX, lowLeftY, lowRightX, lowRightY);
//                      g.drawLine(uppLeftX, uppLeftY, uppRightX, uppRightY);
//                      g.drawLine(uppRightX, uppRightY, lowRightX, lowRightY);

                        drawUpPolygon(g, img, p_top);
                        drawDownPolygon(g, img, p_down);
                        drawBetween(g, img, p_top, p_down);

                        int[] pixel = {255,255,255};
                        img.getRaster().setPixel(t_lowLeftX,t_lowLeftY,pixel);
                        img.getRaster().setPixel(t_uppLeftX, t_uppLeftY,pixel);                 
                        img.getRaster().setPixel(t_lowLeftX, t_lowLeftY, pixel);
                        img.getRaster().setPixel(t_lowRightX, t_lowRightY, pixel);
                        img.getRaster().setPixel(t_uppRightX, t_uppRightY, pixel);

                        img.getRaster().setPixel(d_lowLeftX,d_lowLeftY,pixel);
                        img.getRaster().setPixel(d_uppLeftX, d_uppLeftY,pixel);                 
                        img.getRaster().setPixel(d_lowLeftX, d_lowLeftY, pixel);
                        img.getRaster().setPixel(d_lowRightX, d_lowRightY, pixel);
                        img.getRaster().setPixel(d_uppRightX, d_uppRightY, pixel);


                    };



                    //top polygon's lower left vertex                   

                }
            });


            w.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    w.dispose();
                    g.dispose();
                    System.exit(0);
                }
            });
        }
    }

0 个答案:

没有答案