Tag: 矩阵

从摄像机坐标计算世界坐标

我有一个用2D渲染的世界,我从顶部看它。 Tjat看起来像这样(地砖没有纹理,只有随机的绿色): 在渲染我的实体之前,我像这样变换模型视图矩阵(当position是位置并zoom相机的缩放时, ROTATION为45): glScalef(this.zoom, this.zoom, 1); glTranslatef(this.position.x, this.position.y, 0); glRotatef(ROTATION, 0, 0, 1); 现在我想计算相机当前位置的世界坐标。 我正在尝试用glPushMatrix创建一个新矩阵,然后以与glPushMatrix相同的方式对其进行变换,然后获取矩阵并将给定的摄像机坐标与其相乘: private Vector2f toWorldCoordinates(Vector2f position) { glPushMatrix(); // do the same as when rendering glScalef(this.zoom, this.zoom, 1); glTranslatef(this.position.x, this.position.y, 0); glRotatef(ROTATION, 0, 0, 1); // get the model-view matrix ByteBuffer m = ByteBuffer.allocateDirect(64); m.order(ByteOrder.nativeOrder()); glGetFloatv(GL_MODELVIEW_MATRIX, m); // calculate transformed position […]

两个数组上的Java XOR

我必须在两个数组上应用xor,比如说我有: array_1: 1 0 1 0 1 1 array_2: 1 0 0 1 0 1 我想有一个函数接受两个数组并返回一个应用XOR的数组,所以在这种情况下我希望这个函数返回: returned_array: 0 0 1 1 1 0 请帮我算法..谢谢!

Java矩阵运行时错误

练习信: 给定mxn元素矩阵(m行,n列),以螺旋顺序返回矩阵的所有元素。 例如,给定以下矩阵: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 给定代码: public class Solution { public List spiralOrder(int[][] matrix) { } } 我的代码: public List spiralOrder(int[][] matrix) { if(matrix == null || (matrix.length == 0)) return new ArrayList(); int arriba = […]

通用简单3D矩阵旋转问题

我的3D对象上的旋转有问题(它在java中但它并不重要) 的背景: 我有一个简单的3D模型,你是它上面的第一人称游戏,你向上移动鼠标向上移动(IE按3D的x轴旋转)并向下移动鼠标向下看(向相反方向旋转) 但: 我还有左右箭头键左/右转动(旋转3D的y轴) 现在问题是,当我转动时,当我按x轴旋转时,它不再按预期转动,即如果你转动180度,通过向下移动鼠标实际上是向上看,如果你向上移动鼠标,你实际上看起来下。 我可以在x / y轴上执行什么旋转来修复此问题? – 无论我转动了多少/一点点,向上移动鼠标都会向上移动,向下移动鼠标会向下看。 对不起,我无法更好地解释,但如果您需要更多信息,请发表评论。 非常感谢, 这可能是一个简单的转变,但我想不到:( 一些Java旋转代码: 鼠标向上/向下: public void rotateXY(double radians, double Yradians) { vpTrans.getTransform(T3D); Transform3D test = new Transform3D(); Transform3D testX = new Transform3D(); Transform3D testY = new Transform3D(); translate.set(lastXcord, lastYcord, lastZcord); testX.rotX(radians); testY.rotY(Yradians); test.mul(testX, testY); test.setTranslation(translate); vpTrans.setTransform(test); //System.out.println(test); } // end of rotateXY() 左:(右边是相似但在角度变化时使用减号而不是加号) […]

Matrix转换为JTable

我需要使用静态2D数组填充JTable。 我为JTable创建了这个模型: public class InsertMatToJTable extends AbstractTableModel{ String titre[] = {“age real”, “sex real”, “chest real”, “resting_blood_pressure real”,”serum_cholestoral real”,”fasting_blood_sugar real”,”resting_electrocardiographic_results real”,”maximum_heart_rate_achieved real”,”exercise_induced_angina real”,”oldpeak real”,”slope real”,”number_of_major_vessels real”,”thal real”, “class”}; String line; float mat[][]= new float[270][13]; float matrice_normalise[][]; int i = 0,j=0; public void InsertMatToJTable() { try { FileInputStream fis = new FileInputStream(“fichier.txt”); BufferedReader br = new […]

找到二维数组java的总和

我正在开发一个项目,我必须读取文件并将内容输入到2D数组中。 然后我必须对每行,每列和矩阵的周长求和。 到目前为止,除了周边外,我还有一切工作。 我正在尝试为两个外部列的顶行,底行和中间创建单独的for循环。 矩阵文件如下所示: 1 2 3 4 2 4 6 8 2 4 6 8 3 2 3 4 因此,周长应该加起来为42.现在我可以成功地将第一行和最后一行添加到等于22.但是,当我将列添加到该总数时,我得到32。 这是代码: import java.util.*; // Scanner class import java.io.*; // File class public class Lab10 { static public void main( String [ ] args ) throws Exception { if ( args.length != 1 ) […]

Libgdx如何通过电话方向在多个轴上旋转3D模型

我正在尝试使用手机的加速度计一次在多个轴上旋转3D模型。 当我这样做时,我使用setToRotation()但是,这一次只能做一个轴。 例如: ModelInstance modelInstance = instances.first(); //ROLL modelInstance.transform.setToRotation(Vector3.Z, phoneAccel.y*9); //PITCH modelInstance.transform.setToRotation(Vector3.X, phoneAccel.z*9); 手机处于强制横向模式。 我得到了我想要旋转的模型的实例。 我根据Gdx.input.getAccelerometerX/Y/Z()设置了Vector3 phoneAccel 。 在上面的示例中,两行代码都正常工作,但只能独立工作。 当我尝试使用两者(一个接一个)时,第一次旋转(ROLL)被移除。 我原本认为两个旋转矩阵将累积,即Z轴应用旋转,然后X轴应用旋转。 我是否需要创建自己的累积旋转矩阵,然后在最后应用它? 有什么想法吗? 干杯

通过在自定义视图的onDraw中设置单位矩阵来偏移canvas

我通过canvas.setMatrix(matrix);在自定义视图的onDraw方法中为我的canvas设置了一个矩阵canvas.setMatrix(matrix); 然后我只使用预定义的颜料绘制网格: canvas.drawRect(0,0,viewWidth,viewHeight, background); for(int i=0; i <= nRows; i++){ canvas.drawLine(0,i*cellHeight, nCols*cellWidth,i*cellHeight,lines); canvas.drawLine(i*cellWidth, 0, i*cellWidth, nRows*cellHeight, lines); if(i != nRows) canvas.drawText(""+i, i*cellWidth, (i+1)*cellHeight, text); } 由于某种原因,整个canvas偏移了细胞高度的1.5倍。 知道为什么会发生这种情况或如何解决它? 因此,矩阵未初始化,因此根据文档应该是身份。 非常感谢!

在Java中将2D二进制矩阵转换为黑/白图像

我是java的新手。 我现在有二维矩阵,只有1和0。 我想将它保存为具有相同宽度和高度的jpg图像(黑色和白色)。 我怎么能意识到这一点? 我尝试了下面的代码,但失败了,说“java.lang.IllegalArgumentException:image == null!” 请帮助我,或给我你更好的解决方案。 非常感谢你。 public static void main(String[] args) throws IOException { //result is double[25][33] binary matrix with only 1s and 0s; int height=result.length; int width=result[0].length;; byte[] data = new byte[height*width]; int k=0; for(int i = 0;i < height;i++){ for(int j = 0; j < width; j++){ data[k]=(byte)result[i][j]; k++; } […]

从2Darrays转置矩阵

我自学了一些java,我一直在创建一个用随机值初始化它的2D数组,然后创建数组的转置。 示例输出是: $ java Test1 22 333 44 555 6 Enter the number of rows (1-10): 0 ERROR: number not in specified range (1-10) ! and so on until you enter the correct number of rows and columns. 原始矩阵 1 22 333 44 555 6 转置矩阵 1 333 555` 22 44 6` ^应该是最终输出。 一些帮助代码将不胜感激! 如果行数或列数超出指定范围,我想编码生成错误消息。 […]