how do I do for a cycle in Power BI DAX in measure ?
int n = 3;
double price = 500;
double inflation = 1.02;
for(int i = 1; i <= n; i++)
{
price *= inflation;
}
result is 530.604
I try this:
For Loop =
VAR _n = 3
VAR _price = 500
VAR _inflation = 1.02
VAR _loopTable = GENERATESERIES(1,_n)
VAR _loopTable1 = ADDCOLUMNS(_loopTable,"price",_inflation * SUMX(FILTER(_loopTable,[Value]<=EARLIER([Value])),_price))
VAR _max = MAXX(_loopTable1,[Value])
RETURN
MAXX(FILTER(_loopTable1,[Value]=_max),[_price])
Thank you
Related
Edit by kriegaex: This question is about yaw rotation as opposed to pitch or roll.
So, X = y = z = 0 - mins {105.0, -75.0, 0.0}, maxs {225.0, -15.0, 50.0}
So, that's what im doing (it's wrong, or it's just AABB calculating instead needed stuff).
float fMins[3]; float fMaxs[3];
float yaw = angles2[1] * (3.14 / 180.0);
float xvector[3]; float yvector[3];
xvector[0] = floatcos(yaw);
xvector[1] = floatsin(yaw);
yvector[0] = -floatsin(yaw);
yvector[1] = floatcos(yaw);
float rmin[3] = { 9999.0, 9999.0, 9999.0 };
float rmax[3] = { -9999.0, -9999.0, -9999.0 };
float base[3];
float transformed[3];
for (int i = 0; i <= 1; i++) {
base[0] = i == 0 ? mins[0] : maxs[0];
for (int j = 0; j <= 1; j++) {
base[1] = j == 0 ? mins[1] : maxs[1];
for (int k = 0; k <= 1; k++) {
base[2] = k == 0 ? mins[2] : maxs[2];
transformed[0] = xvector[0]*base[0] + yvector[0]*base[1];
transformed[1] = xvector[1]*base[0] + yvector[1]*base[1];
transformed[2] = base[2];
/*
if (transformed[0] < rmin[0]) rmin[0] = transformed[0];
if (transformed[0] > rmax[0]) rmax[0] = transformed[0];
if (transformed[1] < rmin[1]) rmin[1] = transformed[1];
if (transformed[1] > rmax[1]) rmax[1] = transformed[1];
if (transformed[2] < rmin[2]) rmin[2] = transformed[2];
if (transformed[2] > rmax[2]) rmax[2] = transformed[2];
*/
for (int l = 0; l < 3; l++) {
if (transformed[l] < rmin[l]) rmin[l] = transformed[l];
if (transformed[l] > rmax[l]) rmax[l] = transformed[l];
}
}
}
}
fMins[0] = rmin[0]; fMaxs[0] = rmax[0];
fMins[1] = rmin[1]; fMaxs[1] = rmax[1];
fMins[2] = rmin[2]; fMaxs[2] = rmax[2];
fMins[0] += origin2[0];
fMins[1] += origin2[1];
fMins[2] += origin2[2];
fMaxs[0] += origin2[0];
fMaxs[1] += origin2[1];
fMaxs[2] += origin2[2];
So, on 0 0 0 angle's mins maxs are {105.0, -75.0, 0.0} and {225.0, -15.0, 50.0}.
How i can calculate same sizes on 0 53 0 angles?
I need same sized box, just equalent. Only mins/maxs based solutions.
I am creating a game where i need to move ships at a set speed towards the angle they are facing. I have used this code to move singular ships elsewhere in the game but i assume having them in an array has complicated things.
Any help would be appreciated.
var ship1 = this.addChild(new Ship());
var ship2 = this.addChild(new Ship());
var ship3 = this.addChild(new Ship());
var ship4 = this.addChild(new Ship());
var shipSpeed1 = 10;
var shipArray: Array = [];
shipArray.push(ship1, ship2, ship3, ship4);
for (var i: int = 0; i < shipArray.length; i++) {
var randomX: Number = Math.random() * stage.stageHeight;
var randomY: Number = Math.random() * stage.stageHeight;
shipArray[i].x = randomX;
shipArray[i].y = randomY;
shipArray[i].rotation = 90;
shipArray[i].x += Math.sin(shipArray[i].rotation * (Math.PI / 180)) * shipSpeed1;
shipArray[i].y -= Math.cos(shipArray[i].rotation * (Math.PI / 180)) * shipSpeed1;
}
I've also included this within the same function, but i cant get this to work either. Once again i have had this working
if (shipArray[i].x < 0) { //This allows the boat to leave the scene and
enter on the other side.
shipArray[i].x = 750;
}
if (shipArray[i].x > 750) {
shipArray[i].x = 0;
}
if (shipArray[i].y < 0) {
shipArray[i].y = 600;
}
if (shipArray[i].y > 600) {
shipArray[i].y = 0;
}
First, you need to separate your code into a spawn / initialization phase and an update phase.
Something like the following:
var shipArray: Array = [];
//spawn and set initial values (first phase)
//spawn 4 new ships
var i:int;
for(i=0;i<4;i++){
//instantiate the new ship
var ship:Ship = new Ship();
//add it to the array
shipArray.push(ship);
//set it's initial x/y value
ship.x = Math.random() * (stage.stageWidth - ship.width);
ship.y = Math.random() * (stage.stageHeight - ship.height);
//set it's initial rotation
ship.rotation = Math.round(Math.random() * 360);
//set the ships speed (dynamic property)
ship.speed = 10;
//add the new ship to the display
addChild(ship);
}
//NEXT, add an enter frame listener that runs an update function every frame tick of the application
this.addEventListener(Event.ENTER_FRAME, gameUpdate);
function gameUpdate(e:Event):void {
//loop through each ship in the array
for (var i: int = 0; i < shipArray.length; i++) {
//move it the direction it's rotated, the amount of it's speed property
shipArray[i].x += Math.sin(shipArray[i].rotation * (Math.PI / 180)) * shipArray[i].speed;
shipArray[i].y -= Math.cos(shipArray[i].rotation * (Math.PI / 180)) * shipArray[i].speed;
}
}
I have this code on Google script for get arrays from Sheet1 by criteria in Sheet2 at Sheet3. But now arrays placed only one under the other. What I need is place every new array from 'v' in next 5 columns like in example on my spreadsheet.
Secondly - before this, I used filter with search formula, that allow me use wildcards like * or ?. How I can use wildcards or regexp in my new function?
I would be grateful for any help.
function getval(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var sspodbor = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
var range = ss.getRange("A2:A29");
var values = range.getValues();
var rangez = sheet.getRange("A1:A14");
var valuesz = rangez.getValues();
var z = []
for (var x = 0; x<valuesz.length; x++){
z.push(valuesz[x])
}
var v = [];
for (var q = 0; q < valuesz.length; q++){
for (var s = 0; s < values.length; s++){
if(values[s][5] == z[q]){
v.push([values[s][0],values[s][1],values[s][2],values[s][3],values[s][4]]);
}
//I am guessing that here must be a separating function
}
}
var range = sspodbor.getRange(4, 1, v.length,v[0].length);
range.setValues(v);
}
My spreadsheet: https://docs.google.com/spreadsheets/d/1o7ErbeFHA7yyxMC0HMn3Uj5ZBRcy2uAwa1UpolVpBFI/edit?usp=sharing
Spreading the Groups out Horizontally
It's not the prettiest solution you'll ever see and hopefully others will look it over and make improvements but here it is.
function getval()
{
var Sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var Sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var output = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("output");
var knew = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('knew');
var knewrange = knew.getRange(1,1,10,100);
var pipe = '';
var range1 = Sheet1.getRange("A2:F29");
var values1 = range1.getValues();
var range2 = Sheet2.getRange("A1:A14");
var values2 = range2.getValues();
var z = [];
for (var x = 0; x<values2.length; x++)
{
z.push(values2[x])
}
var v = [];
for (var q = 0; q < values2.length; q++)
{
for (var s = 0; s < values1.length; s++)
{
if(values1[s][5] == z[q])
{
v.push([values1[s][0],values1[s][1],values1[s][2],values1[s][3],values1[s][4],q]);
}
}
}
var vlength=v.length;
var range3 = output.getRange(1, 1, v.length,6);
range3.setValues(v);
var w = [];
var voff = 0;
var hoff = 0;
for(var m=0;m<10;m++)
{
w[m]=[];
for(var n=0;n<100;n++)
{
w[m][n]='' ;
}
}
var color = ['yellow','orange'];
for(var i=0;i<v.length;i++)
{
for(var j=0;j<5;j++)
{
if(i-voff==0){knew.getRange((i-voff + 1),(j + hoff + 1),4,5).setBackground(color[v[i][5] % 2])};
if((i-voff)==0 || v[i][5] == v[i-1][5])
{
w[i - voff][j + hoff]=v[i][j];
}
else
{
voff = i;
hoff += 5;
w[i - voff][j + hoff]=v[i][j];
}
}
}
knewrange.setValues(w);
}
I copied the data from your spreadsheet and the the original getval function. I ended up changing some of the names so I could figure out where to find the data easier. It was a difficult problem for me and one that i enjoyed.
Thanks
I'm writing a game, and I need to make up a rope. I maked it by b2RopeJoint, but there I have not found an opportunity to make it elastic.
Then I looked for b2DistanceJoint, everything is cool with the elasticity, but I can't find an ability to set a limit only to the maximum distance (without minimum one).
How can I do it?
Try this.
-(void) CreateElasticRope {
//=======Params
// Position and size
b2Vec2 lastPos = b2Vec2(4,4); //set position first body
float widthBody = 0.35;
float heightBody = 0.1;
// Body params
float density = 0.05;
float restitution = 0.5;
float friction = 0.5;
// Distance joint
float dampingRatio = 0.0;
float frequencyHz = 0;
// Rope joint
float kMaxWidth = 1.1;
// Bodies
int countBodyInChain = 15;
b2Body* prevBody;
//========Create bodies and joints
for (int k = 0; k < countBodyInChain; k++) {
b2BodyDef bodyDef;
if(k==0 ) bodyDef.type = b2_staticBody; //first body is static
else bodyDef.type = b2_dynamicBody;
bodyDef.position = lastPos;
lastPos += b2Vec2(2*widthBody, 0); //modify b2Vect for next body
bodyDef.fixedRotation = YES;
b2Body* body = world->CreateBody(&bodyDef);
b2PolygonShape distBodyBox;
distBodyBox.SetAsBox(widthBody, heightBody);
b2FixtureDef fixDef;
fixDef.density = density;
fixDef.restitution = restitution;
fixDef.friction = friction;
fixDef.shape = &distBodyBox;
body->CreateFixture(&fixDef);
body->SetHealth(9999999);
body->SetLinearDamping(0.0005f);
if(k>0) {
//Create distance joint
b2DistanceJointDef distJDef;
b2Vec2 anchor1 = prevBody->GetWorldCenter();
b2Vec2 anchor2 = body->GetWorldCenter();
distJDef.Initialize(prevBody, body, anchor1, anchor2);
distJDef.collideConnected = false;
distJDef.dampingRatio = dampingRatio;
distJDef.frequencyHz = frequencyHz;
world->CreateJoint(&distJDef);
//Create rope joint
b2RopeJointDef rDef;
rDef.maxLength = (body->GetPosition() - prevBody->GetPosition()).Length() * kMaxWidth;
rDef.localAnchorA = rDef.localAnchorB = b2Vec2_zero;
rDef.bodyA = prevBody;
rDef.bodyB = body;
world->CreateJoint(&rDef);
} //if k>0
prevBody = body;
} //for
}
There's a 2.2 style ropejoint implementation here: https://github.com/aaronfarr/box2Dweb
You can use both a rope joint and a distance joint together. Make the distance of the rope joint a bit longer than the distance joint.
From the Box2D testbed example, I modified the "Web" example as follows:
#ifndef ELASTICROPE_H
#define ELASTICROPE_H
#define NUM_JOINTS 7
#define NUM_LINKS 8
class ElasticRope : public Test
{
public:
ElasticRope()
{
b2Body* ground = NULL;
{
b2BodyDef bd;
ground = m_world->CreateBody(&bd);
b2EdgeShape shape;
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
ground->CreateFixture(&shape, 0.0f);
}
{
b2CircleShape shape;
shape.m_radius = 0.8f;
// create bodies
for (int b = 0; b < NUM_LINKS; b++) {
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(5.0f, NUM_LINKS-b);
m_bodies[b] = m_world->CreateBody(&bd);
m_bodies[b]->CreateFixture(&shape, 5.0f);
}
for (int j = 0; j < NUM_JOINTS; j++) {
b2DistanceJointDef jd;
b2Vec2 p1, p2, d;
jd.frequencyHz = 5.0f;
jd.dampingRatio = 1.0f;
jd.bodyA = m_bodies[j];
jd.bodyB = m_bodies[j+1];
m_joints[j] = m_world->CreateJoint(&jd);
}
}
}
void Step(Settings* settings)
{
Test::Step(settings);
m_debugDraw.DrawString(5, m_textLine, "This demonstrates an elastic rope.");
m_textLine += DRAW_STRING_NEW_LINE;
}
void JointDestroyed(b2Joint* joint)
{
for (int32 i = 0; i < 8; ++i)
{
if (m_joints[i] == joint)
{
m_joints[i] = NULL;
break;
}
}
}
static Test* Create()
{
return new ElasticRope;
}
b2Body* m_bodies[NUM_LINKS];
b2Joint* m_joints[NUM_JOINTS];
};
#endif // ELASTICROPE_H
Its far from perfect, but may be a starting point.
i stored some mc's in an array.
Now I want to assign coordinates to the mc's in the array in order to put these mc's on the sage at a certain position.
How can I do that?
Thank you for your time
Iterate through your Array of MovieClips using for each()
for each(var i:MovieClip in YOUR_ARRAY)
{
i.x = 17;
i.y = 100;
}
To randomize the positions of the MovieClips:
var min_x:Number = 0;
var max_x:Number = 550;
var min_y:Number = 0;
var max_y:Number = 400;
for each(var i:MovieClip in YOUR_ARRAY)
{
i.x = Math.random() * (max_x-min_x) + min_x;
i.y = Math.random() * (max_y-min_y) + min_y;
}
This can be optimized a bit:
var min_x:Number = 0;
var max_x:Number = 550;
var min_y:Number = 0;
var max_y:Number = 400;
var n:uint = YOUR_ARRAY.length;
for (var i:uint = 0; i < n; i++)
{
var mc:MovieClip = YOUR_ARRAY[i];
mc.x = Math.random() * (max_x-min_x) + min_x;
mc.y = Math.random() * (max_y-min_y) + min_y;
}