Drawing two 3D lines on a canvas?

I need to draw 2 lines on a canvas. Lines should be drawn using the same coordinates , and the second line should be the result of rotating the first line 45 degrees around the Y axis. The result should look like this:

enter image description here

This is my code:

Matrix matrix = new Matrix(); matrix = canvas.getMatrix(); mCamera = new Camera(); canvas.drawText("In the name of God", 30, 100, redPaint); mCamera.rotateY(45); mCamera.getMatrix(matrix); matrix.preTranslate(30, 100); // matrix.postTranslate(-30, -100); canvas.setMatrix(matrix); canvas.drawText("In the name of God", 0, 0, greenPaint); 

But the code result above:

enter image description here

You can see that the coordinates of the lines are different . So what did I do wrong? I assume this is caused by invalid arguments for matrix.preTranslate() .

Update

I am changing my code as follows:

 canvas.drawText("In the name of God", 30, 100, redPaint); mCamera.rotateY(45); mCamera.getMatrix(matrix); matrix.preTranslate(-30, -100); matrix.postTranslate(30, 100); canvas.setMatrix(matrix); canvas.drawText("In the name of God", 0, 0, greenPaint); 

or how:

 canvas.drawText("In the name of God", 30, 100, redPaint); mCamera.rotateY(45); mCamera.getMatrix(matrix); matrix.preTranslate(-30, -100); //matrix.postTranslate(30, 100); canvas.setMatrix(matrix); canvas.drawText("In the name of God", 0, 0, greenPaint); 

or how:

 canvas.drawText("In the name of God", 30, 100, redPaint); mCamera.rotateY(45); mCamera.getMatrix(matrix); matrix.preTranslate(-30, -100); // matrix.postTranslate(30, 100); canvas.setMatrix(matrix); canvas.drawText("In the name of God", 30, 100, greenPaint); 

And for all three of the above codes, the result is as follows:

enter image description here

I assume that the second text is out of range or beyond the status bar , and therefore it is not displayed.

Then change my code to:

 mCamera.rotateY(45); mCamera.getMatrix(matrix); matrix.preTranslate(-30, -100); matrix.postTranslate(30, 100); canvas.setMatrix(matrix); canvas.drawText("In the name of God", 30, 100, greenPaint); 

result:

enter image description here

+4
source share
3 answers

Thanks for answers. I solved the problem. I should use canvas.concat() instead of canvas.setMatrix . This is the correct code:

 Matrix matrix = new Matrix(); mCamera = new Camera(); canvas.drawText("In the name of God", 30, 100, redPaint); mCamera.rotateY(60); mCamera.getMatrix(matrix); matrix.preTranslate(-30, -100); matrix.postTranslate(30, 100); canvas.concat(matrix); canvas.drawText("In the name of God", 30, 100, greenPaint); 
+2
source

Try the following:

 Matrix matrix = new Matrix(); matrix = canvas.getMatrix(); mCamera = new Camera(); canvas.drawText("In the name of God", 30, 100, redPaint); mCamera.translate(30, 100); mCamera.rotateY(45); mCamera.getMatrix(matrix); canvas.setMatrix(matrix); canvas.drawText("In the name of God", 0, 0, greenPaint); 

I never interfere with pre and post translate, but you can perhaps debug it and try to do post-translates instead of pre-broadcast and feed (30, 100) and see if this works.

+2
source

The rotation described by the matrix object always has the property that its axis of rotation passes through the origin. Each point of the axis of rotation is invariant with respect to rotation, which is a bizarre (and compact) way of saying that it does not change. As you described the desired result, you want the left edge of the text to not move. This means that the left edge of the text should be on this invariant axis.

This is for pretranslate() . You need a translation that takes the position of the text to the beginning. The coordinates of this shift are negative in coordinates:

 matrix.preTranslate(-30, -100); 

This translation applies to the text, not to the axis of rotation. This confusion seemed to be the source of your problem. To return the text to where it was before, use postTranslate with the negation of the preliminary translation, that is, with the original coordinates.

To understand this best, read the similarity transformations .

+1
source

Source: https://habr.com/ru/post/1446734/


All Articles