Following Problem: I have a database table with pins coordinates (lat, lng). I send my maps current bounds to the backend to choose visible pins from the database. My code (cakePHP) is following:
private function getMapData($level = 1)
{
$x1 = $this->request->query('x1');
$x2 = $this->request->query('x2');
$y1 = $this->request->query('y1');
$y2 = $this->request->query('y2');
if ($x1 > $x2)
{
$tmp = $x1;
$x1 = $x2;
$x2 = $tmp;
}
if ($y1 > $y2)
{
$tmp = $y1;
$y1 = $y2;
$y2 = $tmp;
}
return $this->Map->find('all',
array(
'fields' => array(
'Map.lat',
'Map.lng',
'Map.level_number'
),
'conditions' => array('Map.level_number' => $level,
'Map.lat BETWEEN ? AND ?' => array($x1, $x2),
'Map.lng BETWEEN ? AND ?' => array($y1, $y2),
),
'group' => 'Map.lat, Map.lng, Map.level_number' //for distinct values
));
}
The query just picks the pins with coordinates in bounds' range. It works most of the time and it doesn't matter which bound's corner comes first - left/right/top/bottom.
But sometimes this simple calculation seems not to work.
I'm getting the bounds and the center point. If I apply the calculation on the center point it's sometimes not within tne bounds which is realy strange but it's really correct data. So I suspect there is something else in this bounds calculation I didn't think about.
here is an example of parameters I get: ?x1=68.62149425024305&y1=150.81473437500006&x2=-16.728397127562125&y2=173.31473437500006&x=36.520571863374705&y=-377.93526562499994&z=3
x1 and y1 is corner point 1
x2, y2 corner point 2
x,y is center
z is zoomlevel
this isn't a good example as center's lng = -377,9.. but I've got requests with coordinates within range lat between -90 and 90 and lng between -180 and 180 and still my calculation fails I get zero data from database and center point which I get from maps api is not within the bounds I get from the same api
When it's so far off you can add 180° and use absolute value:abs(-377.9+180).
Related
I stumbled upon the below question and was unable to solve it, can someone tell whats the approach here
There's no way to do this except by brute force, recursively. For each tile that's not in the right position, there are at most 4 possible swaps to make. You make a swap, then add this new position to the list of ones you haven't tried, making sure not to go back to any position you've seen before. Track the depth of the recursion, and when you get a final position, the depth is the answer.
incoming = (7,3,2,4,1,5,6,8,9)
answer = (1,2,3,4,5,6,7,8,9)
primes = (2,3,5,7,11,13,17)
def solve(base, depth):
seen = set()
untried = [(base,0)]
while untried:
array,depth = untried.pop(0)
print(depth, array)
if array == answer:
print( "ANSWER!", depth )
return depth
if array in seen:
print("seen")
continue
seen.add( array )
for n in range(9):
if array[n] == n+1:
continue
for dx in (-1, -3, 1, 3):
if 0 <= n+dx < 9 and array[n]+array[n+dx] in primes:
# attempt a swap.
a = list(array)
a[n],a[n+dx] = a[n+dx],a[n]
untried.append((tuple(a),depth+1))
print( "fail" )
return -1
solve( incoming, 0 )
I'm writing a DigitalMicrograph script to acquire a mapping data of scattered electron intensities obtained from such as an ADF STEM detector, under various incident-beam conditions being controlled by a handmade script. But, unfortunately I don't know a command to obtain STEM detector signals under non-STEM mode (controlled by DigiScan). What command should I use in this case?
It will be appreciated if you share some wisdom. Thank you very much in advance.
.
As the STEM detector signal is processed by the DigiScan unit, there is no way to read the detector 'signal' independently of it.
Also: You do not get the signal as a "stream" in time, but clocked by the DigiScan. That is, you have to start an acquisition with the DigiScan and can not only "listen" to the detector without one.
However, the DigiScan acquisition is not tied to being in STEM mode. You can start a DigiScan acquisition while being in TEM mode. You can choose the parameters such that acquiring an 'image' is scanning the beam only within a very small area, so that the beam becomes quasi-stationary. Maybe this can help you out?
Here is an example of what I mean: However, I have not tested this on hardware:
// Create "Scan" parameters for an overview
// This image will stay as survey. Its content is not important
// as you're in TEM mode, but we need it as reference
number paramID
number width = 1024 // pixel
number height = 1024 // pixel
number rotation = 0 // degree
number pixelTime= 2 // microseconds
number lSynch = 0 // no-linesync
paramID = DSCreateParameters( width, height, rotation, pixelTime, lSynch )
number signalIndex, dataDepth, selected, imageID
signalIndex = 0 // HAADF (most likely) ?
dataDepth = 4 // 4 byte data
selected = 1 // acquire this signal
imageID = 0 // create new image
DSSetParametersSignal( paramID, signalIndex, dataDepth, selected, imageID )
number continuous = 0 // 0 = single frame, 1 = continuous
number synchronous = 1 // 0 = return immediately, 1 = return when finished
// Capture the "survey" image
DSStartAcquisition( paramID, continuous, synchronous )
image survey := DSGetLastAcquiredImage( signalIndex )
survey.SetName("Survey")
if ( !DSIsValidDSImage( survey ) ) Throw( "Something wrong..")
DSDeleteParameters( paramID ) // remove parameters from memory
// Now we create a "subscan" image for a quasi-stationary beam...
// The size has a minimum size (16x16?) but as we keep the beam
// "stationary" this will rather define your "time-resolution" of
// data. Scan 'speed' is taken from our reference...
number sizeX = 1024
number sizeY = 1024
image Static := IntegerImage( "Static", dataDepth, 0, sizeX, sizeY )
Static.ShowImage()
// defeine "ROI" on the survey. Just the center pixel!
number t,l,b,r
t = height/2
l = width/2
b = t + 1
r = l + 1
DSScanSubRegion( survey, Static, t, l, b, r )
I have a 3d game where I will create an rectangle which is working as screen and the game itself works with vectors to positions. so I will create an rectangle and have only these parameters aviable:
start position ->vector (x,y,z).
Angle(rotation) of object(x,y,z).
size of rectangle.
now also the object need to be roatet to the right side so they are using angels also (x,y,z).
example:
position:-381.968750 -28.653845 -12702.185547
angle: -0.000 90.000 90.000
What I will create is an little bit hard but as idea simple.
I choose 2 complete different positions and angles and will create from the first vector to the second an rectangle.
I can only create an rectangle with the start point and angle.
and I can set the size so (x,y)
So I will now insert 2 positions(vectors) with 2 different angles
The rectangle will have the middle value between the first and second angle so like (90 and 0) -> 45
And the rectangle will start at the start vector and will end with his own size so I don't have a chance to use the end vector directly.
Legendary on photo:
Green=>start and end positions(vectors).
red => the marked zone.
Blue => how I will have the rectangle.
aem_point = vgui.Create( "AEM.Main.Panel" )
if IsValid(aem_point) then
aem_point:SetSize( 2,2 ) -- <-the size that i can set
aem_point:SetPos( 0, 0 )
aem_ph = vgui.Create( "DHTML", aem_point )
aem_ph:SetSize( aem_point:GetSize() )
aem_ph:SetPos(aem_point:GetPos())
aem_ph:SetVisible( true )
aem_ph:SetHTML([[
<html>
<body style="margin:0px;padding:0px;font-size:20px;color:red;border-style: solid;border-color: #ff0000;background-color:rgba(255,0,0,0.1);">
</body>
</html>
]] )
end
hook.Add( "PostDrawOpaqueRenderables", "DrawSample3D2DFrame" .. math.random(), function()
if first and dat_finish then
vgui.Start3D2D( input_position, input_angle, 1 ) -- <-and position&vec
aem_point:Paint3D2D()
vgui.End3D2D()
end
end )
Oh so you want to create a 3d2d plane from 2 vector positions?
Vec1 = A
Vec2 = B
input_position = ( Vec1 + Vec2 ) / 2
A problem you will run into is you need 3 points to generate a plane, so while you can get the position of the screen to get the angle of it you will need another point.
If these screens of yours are staticly set, as in you put their position into the lua code manually and dont intend to have it move or anything, just manually putting in the angle is by far the most simple approch.
As you can see, both of these planes are on the same two points, but they have diffrent angles.
I wrote the demo in expression 2, this should make it clear as to how this works, if you have any other questions just ask.
A = entity(73):pos()
B = entity(83):pos()
if(first())
{
holoCreate(1)
holoCreate(2)
}
holoPos(1,(A+B)/2)
holoScaleUnits(1,vec( abs(B:y() - A:y()) , 1 , abs(sqrt( ( B:z() - A:z() ) ^ 2 + ( B:x() - A:x() ) ^ 2 ))))
holoAng(1,ang(0,90,45))
holoPos(2,(A+B)/2)
holoScaleUnits(2,vec( abs(sqrt( ( B:x() - A:x() ) ^ 2 + ( B:y() - A:y() ) ^ 2 )) , 1 , abs(B:z()-A:z())))
holoAng(2,ang(0,45,0))
How can I find a point ( C (x,y,z) ) between 2 points ( A(x,y,z) , B(x,y,z) ) in a thgree.js scene?
I know that with this: mid point I can find the middle point between them, but I don't want the middle point, I want to find the point which is between them and also has distance a from the A point?
in this picture you can see what I mean :
Thank you.
Basically you need to get the direction vector between the two points (D), normalize it, and you'll use it for getting the new point in the way: NewPoint = PointA + D*Length.
You could use length normalized (0..1) or as an absolute value from 0 to length of the direction vector.
Here you can see some examples using both methods:
Using absolute value:
function getPointInBetweenByLen(pointA, pointB, length) {
var dir = pointB.clone().sub(pointA).normalize().multiplyScalar(length);
return pointA.clone().add(dir);
}
And to use with percentage (0..1)
function getPointInBetweenByPerc(pointA, pointB, percentage) {
var dir = pointB.clone().sub(pointA);
var len = dir.length();
dir = dir.normalize().multiplyScalar(len*percentage);
return pointA.clone().add(dir);
}
See it in action: http://jsfiddle.net/8mnqjsge/
Hope it helps.
I know the question is for THREE.JS and I end up looking for something similar in Babylon JS.
Just in case if you are using Babylon JS Vector3 then the formula would translate to:
function getPointInBetweenByPerc(pointA, pointB, percentage) {
var dir = pointB.clone().subtract(pointA);
var length = dir.length();
dir = dir.normalize().scale(length *percentage);
return pointA.clone().add(dir);
}
Hope it help somebody.
This is known as lerp between two points
e.g. in Three:
C = new Three.Vector3()
C.lerpVectors(A, B, a)
also in generic this is just a single lerp (linear interpolation) math (basically (a * t) + b * (1 - t)) on each axis. Lerp can be described as follows:
function lerp (a, b, t) {
return a + t * (b - a)
}
in your case (see above) :
A = {
x: lerp(A.x, B.x, a),
y: lerp(A.y, B.y, a),
z: lerp(A.z, B.z, a)
}
I am currently working on a simple car simulation on a plane in DirectX. I am having problems on orienting the car on the plane .
what i am doing is this...
D3DXMATRIX planeMat //matrix of the plane on which car is currently situated.
...
//calculating car rotation matrix (on XZ plane )
D3DXMATRIX carRot;
D3DXMatrixRotationY( &carRot , yaw );
//car up will same as plane
D3DXVECTOR3 carUp = D3DXVECTOR3( planeMat._12 , planeMat._22 , planeMat._32 );
//car lookat vector
D3DXVECTOR3 carLookat = D3DXVECTOR3( carRot._13 , carRot._23 , carRot._33 );
// calculating right vector
D3DXVec3Cross( &carRight , &carLookAt , &carUp );
D3DXVec3Normalize( &carRight , &carRight );
//calculating new lookat
D3DXVec3Cross( &carLookAt , &carRight , &carUp );
D3DXVec3Normalize( &carLookAt , &carLookAt );
//car matrix
D3DXMATRIX carMat; D3DXMatrixIdentity( &carMat );
carMat._11 = carRight.x ;carMat._21 = carRight.y ;carMat._31 = carRight.z ;
carMat._12 = carUp.x ;carMat._22 = carUp.y ;carMat._32 = carUp.z ;
carMat._13 = carLookAt.x ;carMat._23 = carLookAt.y ;carMat._33 = carLookAt.z ;
carMat._41 = carPos.x ;carMat._42 = carPos.y ;carMat._43 = carPos.z ;
I can't get car's rotation correct.please help me.
The way you are setting & using components in your matrix doesn't look correct. For instance, in carMat the 3 basis vectors have the 2nd digit of the component constant but in the last one (carPos), the 1st digit of the component is constant. It's either one way or the other for all 4 rows/columns of the matrix.
I believe the way you are setting the car's position (last row/column) is the correct way and the way you are doing the up, right, & lookat are incorrect. So, for instance, it should probably be like this:
D3DXMATRIX carMat; D3DXMatrixIdentity( &carMat );
carMat._11 = carRight.x ;carMat._12 = carRight.y ;carMat._13 = carRight.z ;//11, 12, 13 instead of 11, 21, 31
carMat._21 = carUp.x ;carMat._22 = carUp.y ;carMat._23 = carUp.z ;
carMat._31 = carLookAt.x ;carMat._32 = carLookAt.y ;carMat._33 = carLookAt.z ;
carMat._41 = carPos.x ;carMat._42 = carPos.y ;carMat._43 = carPos.z ;
Also, the way you populate the vectors carUp and carLookAt fom the components of planeMat & carRot suffers from the same issue.