Cannot read property 'add' of null - paperjs

Hi all something wrong with my programming here as getting an error even though shape is drawn, but I can see it;s not quite right sketch
var circle= new Path.Circle({
radius: 100,
position: [200,200]
})
splat= new Path()
splat.fillColor= 'pink'
var count= 20
var length= circle.length
for(var i = 0; i <= count + 1; i++){
var offset= i / count * length
const normal = i === 0 || i === count
? new Point(0, 0)
: circle.getNormalAt(offset) * (Math.random() * 50);
const point = circle.getPointAt(offset).add(i % 2 == 0 ? normal
: -normal);
console.log(point)
splat.add(point)
splat.smooth({ type: 'catmull-rom', factor: 0.5 });
}
Thanks in advance

Your for loop stop condition is wrong:
i <= count + 1 should be either:
i < count + 1
i <= count
Otherwise when i is equal to count + 1, offset value is over length value and circle.getPointAt(offset) returns null.

Related

ZigZag path onMouseDrag with paper.js

Hi im tryign to create a zigzag path using Path.js's onMouseDrag function but getting in to a bit of a muddle here is a sketch
and code
var path
var zigzag
var length
var count
var delta=[]
tool.fixedDistance= 20
function onMouseDown(event){
path= new Path()
path.add(event.point)
zigzag= new Path()
}
function onMouseDrag(event){
event.delta += 90
path.add(event.delta)
delta.push(event.delta)
}
function onMouseUp(event){
length= path.segments.length
zigzag= new Path()
zigzag.add(event.point)
console.log(delta)
delta.forEach(( zig , i) => {
zigzag.add(i % 2 == 0 ? zig + 20 : zig - 20)
})
zigzag.selected= true
}
Based on my previous answer, here is a sketch demonstrating a possible way to do it.
let line;
let zigZag;
function onMouseDown(event) {
line = new Path({
segments: [event.point, event.point],
strokeColor: 'black'
});
zigZag = createZigZagFromLine(line);
}
function onMouseDrag(event) {
line.lastSegment.point = event.point;
if (zigZag) {
zigZag.remove();
}
zigZag = createZigZagFromLine(line);
}
function createZigZagFromLine(line) {
const zigZag = new Path({ selected: true });
const count = 20, length = line.length;
for (let i = 0; i <= count; i++) {
const offset = i / count * length;
const normal = i === 0 || i === count
? new Point(0, 0)
: line.getNormalAt(offset) * 30;
const point = line.getPointAt(offset).add(i % 2 == 0 ? normal
: -normal);
zigZag.add(point);
}
return zigZag;
}

Adding points to Path.Circle first point is at 0,0?

Hello in a for loop whne I add points to a circle the first point is always at 0,0? and not at the beginning of the circle? sketch
var circle= new Path.Circle({
radius: 100,
position: [200,200]
})
splat= new Path.Circle()
splat.fillColor= 'pink'
var count= 40
var length= circle.length
for(var i = 0; i <= count; i++){
var offset= i / count * length
const normal = i === 0 || i === count
? new Point(0, 0)
: circle.getNormalAt(offset) * (Math.random() * 50);
const point = circle.getPointAt(offset).add(i % 2 == 0 ? normal
: -normal);
console.log(point)
splat.add(point)
splat.smooth({ type: 'catmull-rom', factor: 0.5 });
}
splat.closed= true
Thanks in advance
Your path should be created using the generic new Path() constructor and not the specific new Path.Circle() constructor which expects a center and a radius and creates a 0 wide circle at point 0,0 when you're not passing them.
splat= new Path.Circle() => splat= new Path()

javascript fraction to percent

When I tried to convert fraction in Javascript, I am facing a problem.
I need to make a fraction value to percent value.
a = 0.0175
It should be displayed as 1.75%. I tried to multiply by 100. But I am getting some extra fractions added to the right - 1.7500000000000002 . I just need 1.75, not any more zeroes added to the end.
Thanks in advance.
Try this
var a = 0.0175
var num = a * 100;
var n = num.toFixed(2)
multiply, round and divide:
// rounding to set precision or default precision = 3
function roundToPrecision ( value, precision ) {
precision = ( typeof precision !== 'undefined' ) ? precision : 3;
var mult = Math.pow( 10, precision );
return Math.round( mult * value ) / mult;
}
usage:
var zzz = roundToPrecision( 123.1231232546 ); // 123.123
var aaa = roundToPrecision( 123.1231232546, 1 ); // 123.1
Try this:
function formatNum(n) {
return Math.round(n * 1e4) / 1e2;
}
var a = 0.0175;
var n = formatNum(a); // 1.75
var b = 0.21;
var m = formatNum(b); // 21
It simply multiplies by 100*100, rounds the result and divides again by 100. The result is still a number and not a string as with the toFixed-approach.

Find out missing range sequence

I have range sequences. I want to find out is their any missing sequence.
let’s say we have 3 margins 0 -25, 25-50, 75-100
so program gives the result as 50 – 75. is missing sequence.
As others have said you should provide at least some code for us to start off with. Also IMO this has nothing to do with ASP.NET.
The following is the solution which might be of help.
I have assumed a few things
The margins which you are getting can be converted to List<string>
There will be always be only 1 margin missing between any 2 margins. i.e. there will never be a case where your 3 margins are 0 -25, 25-50, 100-125 since here consecutive 2 margins are missing 50-75 and 75-100. If this can be a case then please comment so that I can update my answer accordingly
The first margin in the list is always the start margin
Check out the below code
List<string> margin = new List<string>() { "0-25", "25-50", "100-125" };
List<List<int>> splitedMargin = new List<List<int>>();
foreach (var item in margin)
{
var arr = item.Split('-');
splitedMargin.Add(new List<int>() { int.Parse(arr[0]), int.Parse(arr[1]) });
}
//Required missing margin
List<string> missingMargin = new List<string>();
int marginSize = splitedMargin[0][1] - splitedMargin[0][0];
for (int i = 1; i < splitedMargin.Count; i++)
{
if (splitedMargin[i - 1][1] != splitedMargin[i][0])
{
int missingMarginCount = (splitedMargin[i][0] - splitedMargin[i - 1][1]) / marginSize;
if (missingMarginCount == 1)
missingMargin.Add(splitedMargin[i - 1][1].ToString() + "-" + splitedMargin[i][0].ToString());
else
{
for (int j = 0; j < missingMarginCount; j++)
{
missingMargin.Add((splitedMargin[i - 1][1] + (marginSize * j)).ToString() + "-" + (splitedMargin[i - 1][1] + (marginSize * (j + 1))).ToString());
}
}
}
}
Hope this helps

Converting a decimal to a mixed-radix (base) number

How do you convert a decimal number to mixed radix notation?
I guess that given an input of an array of each of the bases, and the decimal number, it should output an array of the values of each column.
Pseudocode:
bases = [24, 60, 60]
input = 86462 #One day, 1 minute, 2 seconds
output = []
for base in reverse(bases)
output.prepend(input mod base)
input = input div base #div is integer division (round down)
Number -> set:
factors = [52,7,24,60,60,1000]
value = 662321
for i in n-1..0
res[i] = value mod factors[i]
value = value div factors[i]
And the reverse:
If you have the number like 32(52), 5(7), 7(24), 45(60), 15(60), 500(1000) and you want this converted to decimal:
Take number n, multiply it with the factor of n-1, continue for n-1..n=0
values = [32,5,7,45,15,500]
factors = [52,7,24,60,60,1000]
res = 0;
for i in 0..n-1
res = res * factors[i] + values[i]
And you have the number.
In Java you could do
public static int[] Number2MixedRadix(int[] base, int number) throws Exception {
//NB if the max number you want # a position is say 3 then the base# tha position
//in your base array should be 4 not 3
int[] RadixFigures = new int[base.length];
int[] PositionPowers = new int[base.length];
PositionPowers[base.length-1] = 1;
for (int k = base.length-2,pow = 1; k >-1; k--){
pow*=base[k+1];
PositionPowers[k]=pow;
}for (int k = 0; k<base.length; k++){
RadixFigures[k]=number/PositionPowers[k];
if(RadixFigures[k]>base[k])throw new Exception("RadixFigure#["+k+"] => ("+RadixFigures[k]+") is > base#["+k+"] => ("+base[k]+") | ( number is Illegal )");
number=number%PositionPowers[k];
}return RadixFigures;
}
Example
//e.g. mixed-radix base for 1day
int[] base = new int[]{1, 24, 60, 60};//max-day,max-hours,max-minutes,max-seconds
int[] MixedRadix = Number2MixedRadix(base, 19263);//19263 seconds
//this would give [0,5,21,3] => as per 0days 5hrs 21mins 3secs
Reversal
public static int MixedRadix2Number(int[] RadixFigures,int[] base) throws Exception {
if(RadixFigures.length!=base.length)throw new Exception("RadixFigures.length must be = base.length");
int number=0;
int[] PositionPowers = new int[base.length];
PositionPowers[base.length-1] = 1;
for (int k = base.length-2,pow = 1; k >-1; k--){
pow*=base[k+1];
PositionPowers[k]=pow;
}for (int k = 0; k<base.length; k++){
number+=(RadixFigures[k]*PositionPowers[k]);
if(RadixFigures[k]>base[k])throw new Exception("RadixFigure#["+k+"] => ("+RadixFigures[k]+") is > base#["+k+"] => ("+base[k]+") | ( number is Illegal )");
}return number;
}
I came up with a slightly different, and probably not as good method as the other ones here, but I thought I'd share anyway:
var theNumber = 313732097;
// ms s m h d
var bases = [1000, 60, 60, 24, 365];
var placeValues = []; // initialise an array
var currPlaceValue = 1;
for (var i = 0, l = bases.length; i < l; ++i) {
placeValues.push(currPlaceValue);
currPlaceValue *= bases[i];
}
console.log(placeValues);
// this isn't relevant for this specific problem, but might
// be useful in related problems.
var maxNumber = currPlaceValue - 1;
var output = new Array(placeValues.length);
for (var v = placeValues.length - 1; v >= 0; --v) {
output[v] = Math.floor(theNumber / placeValues[v]);
theNumber %= placeValues[v];
}
console.log(output);
// [97, 52, 8, 15, 3] --> 3 days, 15 hours, 8 minutes, 52 seconds, 97 milliseconds
I tried a few of the examples before and found an edge case they didn't cover, if you max out your scale you need to prepend the result from the last step
def intToMix(number,radix=[10]):
mixNum=[]
radix.reverse()
for i in range(0,len(radix)):
mixNum.append(number%radix[i])
number//=radix[i]
mixNum.append(number)
mixNum.reverse()
radix.reverse()
return mixNum
num=60*60*24*7
radix=[7,24,60,60]
tmp1=intToMix(num,radix)

Resources