Matlab point in polygon [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hi found this code on this list and need help converting it to Matlab.
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
nvert: Number of vertices in the polygon. Whether to repeat the first vertex at the end.
vertx, verty: Arrays containing the x- and y-coordinates of the polygon's vertices.
testx, testy: X- and y-coordinate of the test point. (This is from another Stack Overflow question: Point in Polygon aka hit test.
JavaScript version:
function insidePoly(poly, pointx, pointy) {
var i, j;
var inside = false;
for (i = 0, j = poly.length - 1; i < poly.length; j = i++) {
if(((poly[i].y > pointy) != (poly[j].y > pointy)) && (pointx < (poly[j].x-poly[i].x) * (pointy-poly[i].y) / (poly[j].y-poly[i].y) + poly[i].x) ) inside = !inside;
}
return inside;
}
How will this translate in Matlab:
function insidePoly = inpoly(poly, pointx, pointy)
% Code
% return inside

Matlab comes with a build-in function inpolygon which seems to do exactly what you are asking for. No need to reimplement it.

Related

Please help to translate code Pascal to JS? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I have task: On a sheet of paper, a rectangle is drawn along the grid lines. After that, the number of grid nodes located inside, but not on the border of the rectangles, and the number of single grid segments inside the rectangle were counted. Write a program that uses this data to determine the size of a rectangle.
I have this code in Pascal:
var
K,N,i,j:integer;
begin
readln(K,N);
for i:=1 to trunc(sqrt(K)) do
if K mod i = 0 then
begin
if i*(K div i+1)+(K div i)*(i+1)=N then writeln(i+1,' ',K div i+1);
end;
end.
And this my code in JavaScript:
const a = [1000, 2065]
function Sum(K, N) {
for (i = 1; i < Math.trunc(Math.sqrt(K)); i++) {
if (K % i === 0 && i * (Math.floor(K / (i) + 1) + Math.floor(K / i) * (i + 1)) === N) {
break;
}
}
console.log(i + 1, Math.floor(K / (i)) + 1)
}
Sum(a[0], a[1]);
Can you help why my answers in JavaScript are wrong?
Not exactely sure what you're trying to achieve but this javascript code produces the same output (26, 41) as your pascal version does:
See onlinegdb.com!
const K = 1000, N = 2065;
for (let i=1; i<Math.trunc(Math.sqrt(K)); i++)
if (K % i === 0)
if (i*(K / i+1)+(K / i)*(i+1) === N)
console.log(i+1, K / i+1);
I think you have messed something up with the brackets in Math.floor or something similar.

Make a recursive function in JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make a recursive function for this parameters. Function should determine the nth element of the row
a_0 = 1
a_k = k*a_(k-1) + 1/k
k = 1,2,3...
I am trying really hard to do this but i cant get a result. Please help me to do this
I came up with this but it is not a recursive function. I can not do this as a recursive function
let a = 1
let k = 1
let n = 3
for (let i = 1; i<=n; i++){
a = i*a + 1/i
}
console.log(a)
Here's the recursive function you're looking for, it has two conditions:
k == 0 => 1
k != 0 => k * fn(k - 1) + 1/k
function fn(k) {
if(k <= 0) return 1;
return k * fn(k - 1) + 1/k;
}
console.log(fn(1));
console.log(fn(2));
console.log(fn(3));
console.log(fn(4));
Note: I changed the condition of k == 0 to k <= 0 in the actual function so it won't stuck in an infinite loop if you pass a negative k

Calculating trailing zero in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How do i calculate the number of trailing zeros in a factorial of a given number.
N! = 1 * 2 * 3 * 4 ... N
Any Help on this?
Because zeros come from factors 5 and 2 being multiplied together, iterate over all numbers from 1 to the input number, adding to a cumulative count of fives and twos whenever those factors are found. Then, return the smaller of those two counts:
function zeroCount(n) {
let fives = 0;
let twos = 0;
for (let counter = 2; counter <= n; counter++) {
let n = counter;
while (n % 2 === 0) {
n /= 2;
twos++;
}
while (n % 5 === 0) {
n /= 5;
fives++;
}
}
return Math.min(fives, twos);
}
console.log(zeroCount(6)); // 720
console.log(zeroCount(10)); // 3628800
It is very simple, This will help you.
function TrailingZero(n)
{
var c = 0;
for (var i = 5; n / i >= 1; i *= 5)
c += parseInt(n / i);
return c;
}
Let me know if you need help to understand this function.

javascript program without using math. pow, find power of any number using for loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I need to know the code built in for the syntax math.pow(x,y). Actually I used the syntax to find exponent of any number... e.g.
var e = Math.pow(-3, 3); yields -27 but couldn't find out the program behind this... Help me please
If you know what power means..
multiplying the number x n times where x is base and n is exponent.
So you just have to repeat the same thing over and over - and that's why loops are for:
var sum = 1; //note that it's not zero!
for (int i=0;i<n;i++) { //loops n times
sum = sum * x; //on each loop multiplies sum by base number
}
Did you mean alternative for Math.pow? Here is one way with simple loop.
function pow(base,power) {
var p = 1;
for (var i=0; i<power; i++) {
p *= base;
}
return p;
}
You can also use recursion to solve this kind of challenge. Beware that recursion has the disadvantage of increasing space complexity as compared to a for-loop.
function pow(base, power) {
if (power === 1) return base * power
return base * pow(base, power - 1)
}
This is a better way to calculate power of a number with recursion:
function power(base, exp) {
if(exp === 0){
return 1;
}
return base * power(base, exp - 1);
}
You can try this:
function pow(n, e) {
let num = n;
for (let i = 1; i < e; i++) {
num *= n;
}
return num;
}
console.log(pow(-3, 3));
It will give you the required result.

JavaScript code to include Maximum and minimum limit for a number Z [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
function add(id,value)
{
var x = document. get Element By Id(id). value ;
x = x.replace('$','');
x = x.replace(',','');
var y = value;
var z = +x + +y;
document.get Element By Id(id). value =z;
}
To set a minimum value for z as 0 and maximium value as 999999999
If your question is how to make sure z is never less than 0 or greater than 999999999, there are two common ways:
Using if:
if (z < 0) {
z = 0;
}
else if (z > 999999999) {
z = 999999999;
}
Using Math:
z = Math.max(0, Math.min(z, 999999999));
Math.min(z, 999999999) will pick the smallest of the values you give it, and so won't return a value greater than 999999999. Similarly, Math.max(0, ...) will return the largest of the two values you give it, and so won't return a value less than 0.
An obvious solution to this would be
if (z > 999999999) {
z = 999999999;
}else if (z < 0) {
z = 0;
};
Insert this between var z = +x + +y; and document.get Element By Id(id). value =z;

Categories