How can I use JPQL in a NamedQuery to create an entity having a calculated transient attribute? - jpql

I have an SQL statement:
SELECT x.SPEED, (6371 * acos(cos(radians(16.65555)))
* cos(radians(LATITUDE)) * cos(radians(LONGITUDE) - radians(54.55555))
+ sin(radians(16.65555)) * sin(radians(LATITUDE))) AS dist
FROM MY_TABLE x
HAVING dist <= 50
ORDER BY dist
How can I put this into a NamedQuery within a Java entity class in a way that the calculated value is set into this entity as a transient attribute called distance?
For the time being I have tried this:
SELECT vsle,
(:distance_unit * FUNC('acos', FUNC('cos', FUNC('radians', :latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.longitude) - FUNC('radians', :longitude)) +
FUNC('sin', FUNC('radians', :latitude)) * FUNC('sin', FUNC('radians', vsle.geoPosition.latitude)) ) )
AS distance
FROM VehicleStateLogEntity vsle
WHERE (distance <= :radius)
but this fails with a java.lang.NullPointerException. It seems as if the calculated value cannot be accessed via its assigned name distance.

I found a way to build the JPQL valid:
SELECT vsle,
(:distance_unit * FUNC('acos', FUNC('cos', FUNC('radians', :latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.longitude) - FUNC('radians', :longitude)) +
FUNC('sin', FUNC('radians', :latitude)) * FUNC('sin', FUNC('radians', vsle.geoPosition.latitude)) ) ) AS distance
FROM VehicleStateLogEntity vsle
WHERE ((:distance_unit * FUNC('acos', FUNC('cos', FUNC('radians', :latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.longitude) - FUNC('radians', :longitude)) +
FUNC('sin', FUNC('radians', :latitude)) * FUNC('sin', FUNC('radians', vsle.geoPosition.latitude)) ) )<= :radius)
ORDER BY distance
It's weird that I can't access the alias distance in the SELECT-clause but I can in the ORDER BY-clause.

Related

Looping through equations

Is there a way to loop through these calculations and input them into a dataframe as the indicated variable instead of having to calculate them individually. I know repetition is a no-no, but I am not sure how else to do this.
The values of P are constants for different minerals (e.g., P_Cpx, P_Pl) and the D values are constants for different elements and the corresponding mineral (e.g., D_Nb_Cpx).
D_Th_bulk = (P_Cpx * D_Th_Cpx) + (P_Pl * D_Th_Pl) + (P_Opx * D_Th_Opx) + (P_Ol * D_Th_Ol) + (P_Mt * D_Th_Mt) + (P_Ilm * D_Th_Ilm) + (P_Ap * D_Th_Ap) + (P_Chr * D_Th_Chr) + (P_Maj_gn * D_Th_Maj_gn) + (P_Amp * D_Th_Amp)
D_Nb_bulk = (P_Cpx * D_Nb_Cpx) + (P_Pl * D_Nb_Pl) + (P_Opx * D_Nb_Opx) + (P_Ol * D_Nb_Ol) + (P_Mt * D_Nb_Mt) + (P_Ilm * D_Nb_Ilm) + (P_Ap * D_Nb_Ap) + (P_Chr * D_Nb_Chr) + (P_Maj_gn * D_Nb_Maj_gn) + (P_Amp * D_Nb_Amp)
D_La_bulk = (P_Cpx * D_La_Cpx) + (P_Pl * D_La_Pl) + (P_Opx * D_La_Opx) + (P_Ol * D_La_Ol) + (P_Mt * D_La_Mt) + (P_Ilm * D_La_Ilm) + (P_Ap * D_La_Ap) + (P_Chr * D_La_Chr) + (P_Maj_gn * D_La_Maj_gn) + (P_Amp * D_La_Amp)
D_Ce_bulk = (P_Cpx * D_Ce_Cpx) + (P_Pl * D_Ce_Pl) + (P_Opx * D_Ce_Opx) + (P_Ol * D_Ce_Ol) + (P_Mt * D_Ce_Mt) + (P_Ilm * D_Ce_Ilm) + (P_Ap * D_Ce_Ap) + (P_Chr * D_Ce_Chr) + (P_Maj_gn * D_Ce_Maj_gn) + (P_Amp * D_Ce_Amp)
Thank you in advance.

Converting sRGB D65 to CIELab D50

I want to convert from sRGB D65 to CIELab D50. I'm aware of the Bruce Lindbloom functions and calculator but I just want to be sure if I am doing the calculations right.
Starting from a values of sR/100, sG/80, sB/20 and D65, would the following workflow be correct?
sRGB D65 -> XYZ -> Bradford Chromatic adaptation to D50 -> CIE Lab D50 = 34.99, 0.51, 31.35.
There is something not working in the chromatic adaption part of your implementation.
Using colour and a manual conversion:
>>> import colour
>>> import numpy as np
>>> RGB = np.array([100, 80, 20]) / 255
>>> D50 = colour.CCS_ILLUMINANTS['cie_2_1931']['D50']
>>> XYZ = colour.sRGB_to_XYZ(RGB, illuminant=D50)
>>> print(colour.XYZ_to_Lab(XYZ, illuminant=D50))
[ 35.31471609 3.63177851 37.28158403]
And with the Automatic Colour Conversion graph:
>>> colour.convert(RGB, 'sRGB', 'CIE Lab', illuminant=D50) * 100
array([ 35.31471609, 3.63177851, 37.28158403]
And an alternative path that does not use the colour.sRGB_to_XYZ definition and seem to match yours:
>>> colour.convert(RGB, 'Output-Referred RGB', 'CIE Lab', illuminant=D50, verbose={'mode': 'short'}) * 100
===============================================================================
* *
* [ Conversion Path ] *
* *
* "cctf_decoding" --> "RGB_to_XYZ" --> "XYZ_to_Lab" *
* *
===============================================================================
array([ 34.99753019, 0.50577795, 31.35732344])
What is happening though here is that the conversion from RGB to CIE XYZ tristimulus values does not perform any chromatic adaptation between D65 and D50 because the illuminant argument is not matched by the colour.RGB_to_XYZ definition. The proper way to do it would be to specify illuminant_RGB for the RGB side although it defaults to D65 and illuminant_XYZ for the CIE XYZ side:
>>> colour.convert(RGB, 'Output-Referred RGB', 'CIE Lab', illuminant_XYZ=D50, illuminant=D50, verbose={'mode': 'short'}) * 100
===============================================================================
* *
* [ Conversion Path ] *
* *
* "cctf_decoding" --> "RGB_to_XYZ" --> "XYZ_to_Lab" *
* *
===============================================================================
array([ 35.31471609, 3.63177851, 37.28158403])
Now we match the expected conversion result. Here is the verbose conversion so that you can check the intermediate values:
>>> colour.convert(RGB, 'Output-Referred RGB', 'CIE Lab', RGB_to_XYZ={'illuminant_XYZ': D50}, XYZ_to_Lab={'illuminant': D50}, verbose={'mode': 'Extended'}) * 100
===================================================================================
* *
* [ Conversion Path ] *
* *
* "cctf_decoding" --> "RGB_to_XYZ" --> "XYZ_to_Lab" *
* *
===================================================================================
===================================================================================
* *
* [ "cctf_decoding" ] *
* *
* [ Signature ] *
* *
* <Signature (value, function='sRGB', **kwargs)> *
* *
* [ Documentation ] *
* *
* Decodes non-linear :math:`R'G'B'` values to linear :math:`RGB` values using *
* given decoding colour component transfer function (Decoding CCTF). *
* *
* Parameters *
* ---------- *
* value : numeric or array_like *
* Non-linear :math:`R'G'B'` values. *
* function : unicode, optional *
* {:attr:`colour.CCTF_DECODINGS`}, *
* Computation function. *
* *
* Other Parameters *
* ---------------- *
* \**kwargs : dict, optional *
* Keywords arguments for the relevant decoding CCTF of the *
* :attr:`colour.CCTF_DECODINGS` attribute collection. *
* *
* Warnings *
* -------- *
* For *ITU-R BT.2100*, only the electro-optical transfer functions *
* (EOTFs / EOCFs) are exposed by this definition, please refer to the *
* :func:`colour.oetf_inverse` definition for the inverse opto-electronic *
* transfer functions (OETF / OECF). *
* *
* Returns *
* ------- *
* numeric or ndarray *
* Linear :math:`RGB` values. *
* *
* Examples *
* -------- *
* >>> cctf_decoding(0.391006842619746, function='PLog', log_reference=400) *
* ... # doctest: +ELLIPSIS *
* 0.1... *
* >>> cctf_decoding(0.182011532850008, function='ST 2084', L_p=1000) *
* ... # doctest: +ELLIPSIS *
* 0.1... *
* >>> cctf_decoding( # doctest: +ELLIPSIS *
* ... 0.461356129500442, function='ITU-R BT.1886') *
* 0.1... *
* *
* [ Conversion Output ] *
* *
* [ 0.12743768 0.08021982 0.00699541] *
* *
===================================================================================
===================================================================================
* *
* [ "RGB_to_XYZ" ] *
* *
* [ Signature ] *
* *
* <Signature (RGB, illuminant_RGB, illuminant_XYZ, matrix_RGB_to_XYZ, *
* chromatic_adaptation_transform='CAT02', cctf_decoding=None)> *
* *
* [ Filtered Arguments ] *
* *
* {'cctf_decoding': {'return': array([ 0.12743768, 0.08021982, *
* 0.00699541])}, *
* 'illuminant_XYZ': array([ 0.3457, 0.3585])} *
* *
* [ Documentation ] *
* *
* Converts given *RGB* colourspace array to *CIE XYZ* tristimulus values. *
* *
* Parameters *
* ---------- *
* RGB : array_like *
* *RGB* colourspace array. *
* illuminant_RGB : array_like *
* *CIE xy* chromaticity coordinates or *CIE xyY* colourspace array of the *
* *illuminant* for the input *RGB* colourspace array. *
* illuminant_XYZ : array_like *
* *CIE xy* chromaticity coordinates or *CIE xyY* colourspace array of the *
* *illuminant* for the output *CIE XYZ* tristimulus values. *
* matrix_RGB_to_XYZ : array_like *
* Matrix converting the *RGB* colourspace array to *CIE XYZ* tristimulus *
* values, i.e. the *Normalised Primary Matrix* (NPM). *
* chromatic_adaptation_transform : unicode, optional *
* **{'CAT02', 'XYZ Scaling', 'Von Kries', 'Bradford', 'Sharp', *
* 'Fairchild', 'CMCCAT97', 'CMCCAT2000', 'CAT02 Brill 2008', *
* 'Bianco 2010', 'Bianco PC 2010', None}**, *
* *Chromatic adaptation* transform, if *None* no chromatic adaptation is *
* performed. *
* cctf_decoding : object, optional *
* Decoding colour component transfer function (Decoding CCTF) or *
* electro-optical transfer function (EOTF / EOCF). *
* *
* Returns *
* ------- *
* ndarray *
* *CIE XYZ* tristimulus values. *
* *
* Notes *
* ----- *
* *
* +--------------------+-----------------------+---------------+ *
* | **Domain** | **Scale - Reference** | **Scale - 1** | *
* +====================+=======================+===============+ *
* | ``RGB`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* | ``illuminant_XYZ`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* | ``illuminant_RGB`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* *
* +--------------------+-----------------------+---------------+ *
* | **Range** | **Scale - Reference** | **Scale - 1** | *
* +====================+=======================+===============+ *
* | ``XYZ`` | [0, 1] | [0, 1] | *
* +--------------------+-----------------------+---------------+ *
* *
* Examples *
* -------- *
* >>> RGB = np.array([0.45595571, 0.03039702, 0.04087245]) *
* >>> illuminant_RGB = np.array([0.31270, 0.32900]) *
* >>> illuminant_XYZ = np.array([0.34570, 0.35850]) *
* >>> chromatic_adaptation_transform = 'Bradford' *
* >>> matrix_RGB_to_XYZ = np.array( *
* ... [[0.41240000, 0.35760000, 0.18050000], *
* ... [0.21260000, 0.71520000, 0.07220000], *
* ... [0.01930000, 0.11920000, 0.95050000]] *
* ... ) *
* >>> RGB_to_XYZ(RGB, illuminant_RGB, illuminant_XYZ, matrix_RGB_to_XYZ, *
* ... chromatic_adaptation_transform) # doctest: +ELLIPSIS *
* array([ 0.2163881..., 0.1257 , 0.0384749...]) *
* *
* [ Conversion Output ] *
* *
* [ 0.08765592 0.08656689 0.01383652] *
* *
===================================================================================
===================================================================================
* *
* [ "XYZ_to_Lab" ] *
* *
* [ Signature ] *
* *
* <Signature (XYZ, illuminant=array([ 0.3127, 0.329 ]))> *
* *
* [ Filtered Arguments ] *
* *
* {'illuminant': array([ 0.3457, 0.3585])} *
* *
* [ Documentation ] *
* *
* Converts from *CIE XYZ* tristimulus values to *CIE L\*a\*b\** *
* colourspace. *
* *
* Parameters *
* ---------- *
* XYZ : array_like *
* *CIE XYZ* tristimulus values. *
* illuminant : array_like, optional *
* Reference *illuminant* *CIE xy* chromaticity coordinates or *CIE xyY* *
* colourspace array. *
* *
* Returns *
* ------- *
* ndarray *
* *CIE L\*a\*b\** colourspace array. *
* *
* Notes *
* ----- *
* *
* +----------------+-----------------------+-----------------+ *
* | **Domain** | **Scale - Reference** | **Scale - 1** | *
* +================+=======================+=================+ *
* | ``XYZ`` | [0, 1] | [0, 1] | *
* +----------------+-----------------------+-----------------+ *
* | ``illuminant`` | [0, 1] | [0, 1] | *
* +----------------+-----------------------+-----------------+ *
* *
* +----------------+-----------------------+-----------------+ *
* | **Range** | **Scale - Reference** | **Scale - 1** | *
* +================+=======================+=================+ *
* | ``Lab`` | ``L`` : [0, 100] | ``L`` : [0, 1] | *
* | | | | *
* | | ``a`` : [-100, 100] | ``a`` : [-1, 1] | *
* | | | | *
* | | ``b`` : [-100, 100] | ``b`` : [-1, 1] | *
* +----------------+-----------------------+-----------------+ *
* *
* References *
* ---------- *
* :cite:`CIETC1-482004m` *
* *
* Examples *
* -------- *
* >>> import numpy as np *
* >>> XYZ = np.array([0.20654008, 0.12197225, 0.05136952]) *
* >>> XYZ_to_Lab(XYZ) # doctest: +ELLIPSIS *
* array([ 41.5278752..., 52.6385830..., 26.9231792...]) *
* *
* [ Conversion Output ] *
* *
* [ 0.35314716 0.03631779 0.37281584] *
* *
===================================================================================
array([ 35.31471609, 3.63177851, 37.28158403])

Haskell minigame: check solvability of a maze (updated)

check :: String -> Int -> Int -> IO()
check map len n = do
let current = searchMap map '#'
if (flood map current Up len n)
then printf "\nThis map is solvable. \n"
else printf "\nThis map is unsolvable. \n"
flood :: String -> Int -> Direction -> Int -> Int -> Bool
flood map current direct len n = do
let upward = current - n
let downward = current + n
let leftward = current - 2
let rightward = current + 2
if (current < 0 || current > len || (map !! current) == '*' || (map !! current) == 'x')
then False
else if (map !! current) == 't'
then True
else do
case direct of
Up -> do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map leftward Leftx len n) || (flood map rightward Rightx len n)
Down -> do
let newMap = replaceSign map current "x"
(flood map downward Down len n) || (flood map leftward Leftx len n) || (flood map rightward Rightx len n)
Leftx -> do
let row = findRow current n 0
if current < (row * n)
then False
else do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map downward Down len n) || (flood map leftward Leftx len n)
Rightx -> do
let row = findRow current n 0
if current >= ((row + 1) * n)
then False
else do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map downward Down len n) || (flood map rightward Rightx len n)
I am working on a project which needs to make a minigame. The player will need to input a map.
Something like this:
* * * * * - - - - - - - - - - - - - - - - - - - * * * * *
* * * * * b - - - - - - - - - - - - - - - - - b * * * * *
* * * * * - * * * * * * * * * * * * * * * * * - * * * * *
* * * * * - * * - - - - * * * * * - - - - * * - * * * * *
* * * * * - * * - y y - * * * * * - y y - * * - * * * * *
* * * * * - * * - - - - * * * * * - - - - * * - * * * * *
* * * * * - * * * * * * - - b - - * * * * * * - * * * * *
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
# - - - - - * * * * * * - * * * - * * * * * * p - - - - t
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
* * * * * - - - - - - - - * * * - - - - - - - - * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
I struggling in a function to determine whether the map is solvable or not.
In the code, current means the current location the program is trying. len is the length of the String holding the map. n is the length of one line of the map. In the map, # means the ball. - means roads that the ball can move on. b means bonus block (not related to this problem). * means obstacle. t means target p/o/y means blocks with special use (ball can stop there). My current approach is to use a function try to try recursively. The function will turn roads that have already tried into x and the function will return False if it hit x. When function hits obstacles (*) or boundaries of map it will try another two directions. Eg. if you are currently moving Up. Then it will try Leftx and Rightx. If it hits special blocks (p/o/y), it will try the other three directions. Eg. if you are currently moving Rightx, then it will try Up, Down, and Rightx. However, the program returns "Exception: stack overflow". Is it something wrong with my algorithm? How can I fix this?
The problem is that once your search has proceeded all the way right and up, so the map looks like this, with the current position the leftmost - in the first row:
* * * * * - - - - - - - - - - - - - - - - - - - * * * * *
* * * * * x - - - - - - - - - - - - - - - - - b * * * * *
* * * * * x * * * * * * * * * * * * * * * * * - * * * * *
* * * * * x * * - - - - * * * * * - - - - * * - * * * * *
* * * * * x * * - y y - * * * * * - y y - * * - * * * * *
* * * * * x * * - - - - * * * * * - - - - * * - * * * * *
* * * * * x * * * * * * - - b - - * * * * * * - * * * * *
* * * * * x * * * * * * - * * * - * * * * * * - * * * * *
x x x x x x * * * * * * - * * * - * * * * * * p - - - - t
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
* * * * * - - - - - - - - * * * - - - - - - - - * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
you enter the following loop with no further x marks made to the map:
try map 10 Up ... =
-- at top row, so we evaluate:
(try map current Leftx len n) || ...
try map 10 Leftx ... =
-- star to the left, so we evaluate:
(try map current Up len n) || ...
try map 10 Up ... =
-- at top row, so we evaluate:
(try map current Leftx len n) || ...
-- and we're stuck in an infinite loop
The stack eventually overflows because Haskell is keeping track of all those right-hand sides of the || operator, though it never gets a chance to try any of them.
I think you'll need to either modify your algorithm to mark walls you've already tried, or maybe switch from working with a single path to flood filling all the non-stars -- if you hit the target while flood fillling, the maze is solvable.

How to get current camera position in PyOpenGL?

I am making a game in OpenGL which some objects goes toward you. To do that, I have to get the current camera location. I know that in OpenGL the camera always stay at (0,0,0) position, but I want to get the current location of the camera according to the world. How can I do that>
I've tried viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
camera_pos = viewMatrix[3], but my camera have pitch and yaw and the position always change when I am doing that. How can I solve this problem?
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math,random
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_p:
paused = not paused
if not paused:
if event.type == pygame.MOUSEMOTION:
mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
pygame.mouse.set_pos(displayCenter)
pygame.mouse.set_visible(False)
if not paused:
#Get keys
keypress = pygame.key.get_pressed()
#Init model view matrix
glLoadIdentity()
#------------------------View------------------------
#Apply the look up and down (with 90° angle limit)
if up_down_angle < -90:
if mouseMove[1] > 0:
up_down_angle += mouseMove[1]*0.1
elif up_down_angle > 90:
if mouseMove[1] < 0:
up_down_angle += mouseMove[1]*0.1
else:
up_down_angle += mouseMove[1]*0.1
glRotatef(up_down_angle, 1.0, 0.0, 0.0)
#Init the view matrix
glPushMatrix()
glLoadIdentity()
#Apply the movement
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)
#Apply the look left and right
glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)
#------------------------View------------------------
#------------------------Draw------------------------
#Multiply the current matrix by the new view matrix and store the final view matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
camera_pos = viewMatrix[3] #Incorrect
print(camera_pos) #print the output
#Apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
glLightfv(GL_LIGHT0, GL_POSITION, [1, -1, 1, 0])
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glColor4f(0.2, 0.2, 0.5, 1)
for person in persons:
person.draw()
ground.draw()
glutSwapBuffers()
glPopMatrix()
#------------------------Draw------------------------
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()
I expect when I do the view rotation my coordinates won't move, but the output of camera_pos changes when doing that.
The view matrix transforms form world space to view space. This means the view matrix is not the matrix which contains the camera position. The view matrix is the inverse matrix which contains the camera position and orientation.
Write a function which can can calculate the inverse matrix of a 4x4 matrix:
def InverseMat44(mat):
m = [mat[i][j] for i in range(4) for j in range(4)]
inv = [0]*16
inv[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10]
inv[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10]
inv[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9]
inv[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9]
inv[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10]
inv[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10]
inv[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9]
inv[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9]
inv[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6]
inv[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] - m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6]
inv[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5]
inv[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] - m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5]
inv[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] - m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6]
inv[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6]
inv[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] - m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5]
inv[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5]
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12]
for i in range(16):
inv[i] /= det;
return inv;
See also inverting a 4x4 matrix and Find the inverse of a 4×4 matrix.
And calculate the inverse matrix of the view matrix:
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
invVM = InverseMat44(viewMatrix)
camera_pos = (invVM[12], invVM[13], invVM[14])
print(camera_pos)
The inverse matrix can also be calculated by libraries. e.g. numpy.linalg.inv from NumPy library or glm.inverse from PyGLM library.

how to print FlippedTriangle pattern using python

Here is my code but it is not working as expected
def printFlippedTriangle(width):
for i in range(0, width):
for J in range(0, width-i):
print(" ", end=" ") # single line
for j in range(0,i):
print(" "+"* ", end=" ") # single line
j=j-1
print("*")
Am getting this:
*
* * * * *
* * * * * * *
* * * * * * *
* * * * *
am suppose to get:
*
* *
* * *
* * * *
* * * * *
Any idea and or suggestion will be appreciated
This will get the job done, and in a single loop too!
def triangle(w):
for i in range(0, w):
print(' ' * ((w - i - 1) * 2), end='') # spaces for each row
print('* ' * (i + 1), end='') # * for each row
print() # new line
>>> triangle(5)
*
* *
* * *
* * * *
* * * * *
Each row needs width - rowNumber - 1 spaces and rowNumber + 1 asterisks when starting from 0

Resources