Announcement

Collapse
No announcement yet.

Incorrect calculations, Can't found the cause.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Incorrect calculations, Can't found the cause.

    I've start studing BP yesterday. I already did some tutorials but none of them can help me with it. I'm trying to subdivide a line (a trunk in this case) to few pieces with a random size. This code have two inputs: Subdivision ( which is an integer - number of subdivisions) and Length ( length of the line). I implemented this code in JS to check how it's works and have an actual code at front of me, but when I did it in BP I've get a wrong results. Could you explain to me where I've get a mistake? Thanks everybody for any help.

    HERE IS MY BP:
    Click image for larger version

Name:	BP_SubdTrk.png
Views:	1
Size:	593.8 KB
ID:	1191708

    HERE IS THE CODE WHICH I TRY TO IMPLEMENT:
    Code:
    var height = 100,
        subd = 5,
        divArr = [];
    
    for(i = 0; i < subd; i++) {
      if(i < subd-1) {
        var div = height / (subd - i),
            divMax = div + div * 0.5,
            divMin = div *  0.5,
            randDiv = Math.random() * (divMax - divMin) + divMin;
        divArr[i] = randDiv;
        height -= randDiv;
      } else {
        divArr[i] = height;
      }
    }
    
    // show result of random length
    console.log(divArr)
    
    var  a = 0;
    for(i = 0; i < divArr.length; i++) {
      a+=divArr[i];
    }
    // check that numbers in array in total have the same result as it was declared in the begin
    console.log(a)
    HERE IS THE LINK TO JS CODE (same as above just with result in console):
    http://jsbin.com/xakeliqedi/edit?js,console
    Last edited by MisaGu; 08-15-2016, 02:27 AM.

    #2
    Here I fix a little bug, there I forgot to decrease divider by 1 for each iteration of the loop. ( ex.: 5,4,3,2,1 )
    Click image for larger version

Name:	BP_SubdTrk_bf_1.png
Views:	2
Size:	643.7 KB
ID:	1113764

    But result still incorrect:
    LogBlueprintUserMessages: [LineSubdevision_C_0] Length: 2000.0
    LogBlueprintUserMessages: [LineSubdevision_C_0] Index: 0; Val: 354.106262
    LogBlueprintUserMessages: [LineSubdevision_C_0] Index: 1; Val: 588.507874
    LogBlueprintUserMessages: [LineSubdevision_C_0] Index: 2; Val: 568.182556
    LogBlueprintUserMessages: [LineSubdevision_C_0] Index: 3; Val: 223.561554
    LogBlueprintUserMessages: [LineSubdevision_C_0] Index: 4; Val: 424.24054
    LogBlueprintUserMessages: [LineSubdevision_C_0] LinSudbResult Total: 2158.598633
    As you can see LinSudbResult not equal Length , !@#$%
    Last edited by MisaGu; 08-15-2016, 04:37 AM.

    Comment


      #3
      Fix a problem.

      Comment


        #4
        Try subdivisons - 2
        It's i < subd - 1, not i <= subd-1
        @CarstenZarbock - Follow me on Twitter, thank you

        Comment


          #5
          I'm curious, did it work?
          @CarstenZarbock - Follow me on Twitter, thank you

          Comment

          Working...
          X