我需要使用java扩展2d方块(I need to scale a 2d square using java)

编程入门 行业动态 更新时间:2024-10-14 18:15:10
我需要使用java扩展2d方块(I need to scale a 2d square using java)

我正在编写一个程序来转换,旋转和缩放2d方块。 我有转换和旋转工作,但我需要帮助缩放。 我似乎无法在互联网上找到任何帮助,因为我必须使用数学方程,我找不到所需的方程式。 只是因为你知道我不想使用gl.glScaled()。 我需要使用数学方程,但我无法弄清楚。

package lab2; public class Square { public double [][] vertices = new double[4][2]; public Square(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { vertices[0][0]=x1; vertices[0][1]=y1; vertices[1][0]=x2; vertices[1][1]=y2; vertices[2][0]=x3; vertices[2][1]=y3; vertices[3][0]=x4; vertices[3][1]=y4; } public double area() { return (vertices[1][0]-vertices[0][0])*(vertices[1][0]-vertices[0][0])+(vertices[1][1]-vertices[0][1])*(vertices[1][1]-vertices[0][1]); } public void translate(double tx, double ty) { for(int i=0;i<vertices.length;i++) { vertices[i][0]+=tx; vertices[i][1]+=ty; } } public void rotate(double theta) { //double x =0; double x = (vertices[0][0]+vertices[2][0])/2; double y = (vertices[0][1]+vertices[2][1])/2; double oldX; int i; for(i = 0; i < 4; i++){ oldX = vertices[i][0]; vertices[i][0] = x + (vertices[i][0]-x)*Math.cos(theta*0.0174532925199)-(vertices[i][1]- y)*Math.sin(theta*0.0174532925199); vertices[i][1] = y + (oldX-x)*Math.sin(theta*0.0174532925199)+(vertices[i][1]-y)*Math.cos(theta*0.0174532925199); } } public void scale(double sx, double sy) { } }

然后我也有这个SquareControl.java,以便我可以控制我希望它如何工作所以对于这个程序我将使用关键事件

package lab2; import java.awt.Frame; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.media.opengl.*; import javax.media.opengl.awt.GLCanvas; import com.sun.opengl.util.Animator; import com.sun.opengl.util.FPSAnimator; public class Square_Control implements GLEventListener, KeyListener { Square square = new Square(100,100,200,100,200,200,100,200); boolean rotating = false; boolean scaling = false; boolean enlarge = true; double theta = 1; double sx = 1.01, sy = 1.01; GLProfile glp; GLCapabilities caps; GLCanvas canvas; public Square_Control() { glp = GLProfile.getDefault(); caps = new GLCapabilities(glp); canvas = new GLCanvas(caps); Frame frame = new Frame("AWT Window Test"); frame.setSize(300, 300); frame.add(canvas); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); canvas.addGLEventListener(this); canvas.addKeyListener(this); canvas.requestFocus(); Animator animator = new FPSAnimator(canvas,60); animator.add(canvas); animator.start(); } public static void main(String[] args) { Square_Control sqc = new Square_Control(); } public void update() { if (rotating) square.rotate(theta); if (scaling) { square.scale(1, 1); } } @Override public void display(GLAutoDrawable drawable) { update(); GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glColor3f(1, 0, 0); gl.glBegin(GL.GL_LINE_LOOP); gl.glVertex2d(square.vertices[0][0], square.vertices[0][1]); gl.glVertex2d(square.vertices[1][0], square.vertices[1][1]); gl.glVertex2d(square.vertices[2][0], square.vertices[2][1]); gl.glVertex2d(square.vertices[3][0], square.vertices[3][1]); gl.glEnd(); } @Override public void dispose(GLAutoDrawable drawable) { // TODO Auto-generated method stub } @Override public void init(GLAutoDrawable drawable) { // TODO Auto-generated method stub GL2 gl = drawable.getGL().getGL2(); gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho(0, 300, 0, 300, -1, 1); gl.glViewport(0, 0, 300, 300); } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent arg0) { if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) square.translate(4, 0); else if (arg0.getKeyCode() == KeyEvent.VK_LEFT) square.translate(-4, 0); else if (arg0.getKeyCode() == KeyEvent.VK_UP) square.translate(0, 4); else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) square.translate(0, -4); //also add code to toggle rotation/scaling if(arg0.getKeyCode() == KeyEvent.VK_R) { rotating = !rotating; } if(arg0.getKeyCode() == KeyEvent.VK_S) { scaling = !scaling; } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }

I'm writing a program to transform, rotate and scale a 2d square. I have the transformation and rotation working but I need help with the scaling. I can't seem to find any help on the internet to help since I have to use a math equation and I can't find the equation needed. Just so you know I don't want to use gl.glScaled(). I need to use a math equation but I can't figure it out.

package lab2; public class Square { public double [][] vertices = new double[4][2]; public Square(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { vertices[0][0]=x1; vertices[0][1]=y1; vertices[1][0]=x2; vertices[1][1]=y2; vertices[2][0]=x3; vertices[2][1]=y3; vertices[3][0]=x4; vertices[3][1]=y4; } public double area() { return (vertices[1][0]-vertices[0][0])*(vertices[1][0]-vertices[0][0])+(vertices[1][1]-vertices[0][1])*(vertices[1][1]-vertices[0][1]); } public void translate(double tx, double ty) { for(int i=0;i<vertices.length;i++) { vertices[i][0]+=tx; vertices[i][1]+=ty; } } public void rotate(double theta) { //double x =0; double x = (vertices[0][0]+vertices[2][0])/2; double y = (vertices[0][1]+vertices[2][1])/2; double oldX; int i; for(i = 0; i < 4; i++){ oldX = vertices[i][0]; vertices[i][0] = x + (vertices[i][0]-x)*Math.cos(theta*0.0174532925199)-(vertices[i][1]- y)*Math.sin(theta*0.0174532925199); vertices[i][1] = y + (oldX-x)*Math.sin(theta*0.0174532925199)+(vertices[i][1]-y)*Math.cos(theta*0.0174532925199); } } public void scale(double sx, double sy) { } }

Then I also have this SquareControl.java so that I can control how I want it to work so for this program I'll be using key events

package lab2; import java.awt.Frame; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.media.opengl.*; import javax.media.opengl.awt.GLCanvas; import com.sun.opengl.util.Animator; import com.sun.opengl.util.FPSAnimator; public class Square_Control implements GLEventListener, KeyListener { Square square = new Square(100,100,200,100,200,200,100,200); boolean rotating = false; boolean scaling = false; boolean enlarge = true; double theta = 1; double sx = 1.01, sy = 1.01; GLProfile glp; GLCapabilities caps; GLCanvas canvas; public Square_Control() { glp = GLProfile.getDefault(); caps = new GLCapabilities(glp); canvas = new GLCanvas(caps); Frame frame = new Frame("AWT Window Test"); frame.setSize(300, 300); frame.add(canvas); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); canvas.addGLEventListener(this); canvas.addKeyListener(this); canvas.requestFocus(); Animator animator = new FPSAnimator(canvas,60); animator.add(canvas); animator.start(); } public static void main(String[] args) { Square_Control sqc = new Square_Control(); } public void update() { if (rotating) square.rotate(theta); if (scaling) { square.scale(1, 1); } } @Override public void display(GLAutoDrawable drawable) { update(); GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glColor3f(1, 0, 0); gl.glBegin(GL.GL_LINE_LOOP); gl.glVertex2d(square.vertices[0][0], square.vertices[0][1]); gl.glVertex2d(square.vertices[1][0], square.vertices[1][1]); gl.glVertex2d(square.vertices[2][0], square.vertices[2][1]); gl.glVertex2d(square.vertices[3][0], square.vertices[3][1]); gl.glEnd(); } @Override public void dispose(GLAutoDrawable drawable) { // TODO Auto-generated method stub } @Override public void init(GLAutoDrawable drawable) { // TODO Auto-generated method stub GL2 gl = drawable.getGL().getGL2(); gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho(0, 300, 0, 300, -1, 1); gl.glViewport(0, 0, 300, 300); } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent arg0) { if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) square.translate(4, 0); else if (arg0.getKeyCode() == KeyEvent.VK_LEFT) square.translate(-4, 0); else if (arg0.getKeyCode() == KeyEvent.VK_UP) square.translate(0, 4); else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) square.translate(0, -4); //also add code to toggle rotation/scaling if(arg0.getKeyCode() == KeyEvent.VK_R) { rotating = !rotating; } if(arg0.getKeyCode() == KeyEvent.VK_S) { scaling = !scaling; } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }

最满意答案

要缩放对象,只需将每个顶点乘以缩放系数即可。 缩放因子1.0将不起作用,而缩放因子2.0将使每个顶点的位置加倍,因此缩放它并可能翻译它。

如果您希望对象保持原位,则必须先将其转换为对象的中心,缩放,然后转换回原始位置。

那就是你想要做的工作软件。

您应该考虑使用Matrix函数。 glRotatef,glTranslatef和glScalef。

To scale an object, you simply have to multiply each vertex by your scaling factor. A scaling factor of 1.0 will do nothing while a scaling factor of 2.0 will double de position of each vertex, hence scaling it AND probably translating it.

If you want the object to stay in place, you'll have to first translate it to your object's center, scale, then translate back to it's original position.

That is if you want to do the job software.

You should consider using Matrix functions. glRotatef, glTranslatef and glScalef.

更多推荐

本文发布于:2023-08-06 19:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方块   java   square   scale

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!