Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38011 > unrolled thread

Refreshing grade school math.

Started byJonas Thörnvall <jonas.thornvall@gmail.com>
First post2020-01-08 23:54 -0800
Last post2020-01-13 10:04 -0800
Articles 20 on this page of 21 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-08 23:54 -0800
    Re: Refreshing grade school math. "R.Wieser" <address@not.available> - 2020-01-09 09:19 +0100
      Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-09 00:34 -0800
        Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-09 00:43 -0800
          Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-09 00:44 -0800
      Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-11 05:09 -0800
        Re: Refreshing grade school math. Julio Di Egidio <julio@diegidio.name> - 2020-01-11 22:13 -0800
          Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 09:58 -0800
            Re: Refreshing grade school math. Julio Di Egidio <julio@diegidio.name> - 2020-01-13 10:16 -0800
              Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 11:16 -0800
            Re: Refreshing grade school math. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-13 12:22 -0800
              Re: Refreshing grade school math. Julio Di Egidio <julio@diegidio.name> - 2020-01-13 12:53 -0800
                Re: Refreshing grade school math. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-13 15:31 -0800
                Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 16:03 -0800
              Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 13:00 -0800
                Re: Refreshing grade school math. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-13 15:35 -0800
                  Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 15:43 -0800
              Re: Refreshing grade school math. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-01-14 01:01 +0100
                Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 16:07 -0800
          Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 10:01 -0800
          Re: Refreshing grade school math. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-01-13 10:04 -0800

Page 1 of 2  [1] 2  Next page →


#38011 — Refreshing grade school math.

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-08 23:54 -0800
SubjectRefreshing grade school math.
Message-ID<8fe5af4b-242f-460f-b546-4a2ed830d1eb@googlegroups.com>
I decided to try make a function to draw a circle "polygon" without angles. But i have completly forgotten howto evenly distribute the cordinates without using sin cosin angle.

Below is the circle/polygon divided into four quadrants and arched. But it does not create a regular polygon. Simple fact my math skills lacking. Before i decided to graph equation, i first did try solve the cordinates using purely geometric approach. No angles just finding cross sections of shape, couldn't do it.

But maybe someone with a little more math insight have an idea howto create regular polygons without cosin and sin angle?

Distance is "sort of" vertex distance but it is not regular.

function circle(){
translateX=800;
translateY=450;
radius=400;
x=400;
distance=150;
arclength=280;
//x*x+y*y=400*400  solve y
for (x=-arclength;x<=arclength;x=x+distance){
ysquare=(radius*radius)-(x*x);
y=Math.sqrt(ysquare);
if(x==-arclength){savex=x;savey=y;}
ctx.fillStyle="black";
ctx.lineTo(x+translateX,y+translateY);
//ctx.fillRect(x+translateX,y+translateY,4,4); 
}
for (y=arclength;y>=-arclength;y=y-distance){
xsquare=(radius*radius)-(y*y);
x=Math.sqrt(xsquare);
ctx.lineTo(x+translateX,y+translateY);
//ctx.fillRect(x+translateX,y+translateY,3,3); 
}
for (x=arclength;x>=-arclength;x=x-distance){
ysquare=(radius*radius)-(x*x);
y=Math.sqrt(ysquare);
ctx.lineTo(x+translateX,-y+translateY);
//ctx.fillRect(x+translateX,-y+translateY,3,3); 
}
for (y=-arclength;y<=arclength;y=y+distance){
xsquare=(radius*radius)-(y*y);
x=Math.sqrt(xsquare);
ctx.lineTo(-x+translateX,y+translateY);
//ctx.fillRect(-x+translateX,y+translateY,3,3); 
}
ctx.lineTo(savex+translateX,savey+translateY);
ctx.fillStyle="yellow";
ctx.stroke();
}

[toc] | [next] | [standalone]


#38012

From"R.Wieser" <address@not.available>
Date2020-01-09 09:19 +0100
Message-ID<qv6nnc$h44$1@gioia.aioe.org>
In reply to#38011
Jonas,

Your google-fu is weak.

A quick search came up with this approach:

https://forums.parallax.com/discussion/161967/minsky-circle-algorithm-generate-sin-and-cos-fast-with-only-add-sub-and-shift

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#38013

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-09 00:34 -0800
Message-ID<b737bdda-d7b0-481d-a47d-0887a0609dd1@googlegroups.com>
In reply to#38012
Den torsdag 9 januari 2020 kl. 09:20:03 UTC+1 skrev R.Wieser:
> Jonas,
> 
> Your google-fu is weak.
> 
> A quick search came up with this approach:
> 
> https://forums.parallax.com/discussion/161967/minsky-circle-algorithm-generate-sin-and-cos-fast-with-only-add-sub-and-shift
> 
> Regards,
> Rudy Wieser

I said regular polygon that one is "much" worse then my approach?

[toc] | [prev] | [next] | [standalone]


#38014

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-09 00:43 -0800
Message-ID<218025de-0a4a-4126-b9d8-48043388bb91@googlegroups.com>
In reply to#38013
Den torsdag 9 januari 2020 kl. 09:34:46 UTC+1 skrev Jonas Thörnvall:
> Den torsdag 9 januari 2020 kl. 09:20:03 UTC+1 skrev R.Wieser:
> > Jonas,
> > 
> > Your google-fu is weak.
> > 
> > A quick search came up with this approach:
> > 
> > https://forums.parallax.com/discussion/161967/minsky-circle-algorithm-generate-sin-and-cos-fast-with-only-add-sub-and-shift
> > 
> > Regards,
> > Rudy Wieser
> 
> I said regular polygon that one is "much" worse then my approach?


What i want is x dots evenly spaced around circle cordinates.

[toc] | [prev] | [next] | [standalone]


#38015

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-09 00:44 -0800
Message-ID<35f21e2d-53a2-4eba-88c5-875955cd809b@googlegroups.com>
In reply to#38014
Den torsdag 9 januari 2020 kl. 09:43:15 UTC+1 skrev Jonas Thörnvall:
> Den torsdag 9 januari 2020 kl. 09:34:46 UTC+1 skrev Jonas Thörnvall:
> > Den torsdag 9 januari 2020 kl. 09:20:03 UTC+1 skrev R.Wieser:
> > > Jonas,
> > > 
> > > Your google-fu is weak.
> > > 
> > > A quick search came up with this approach:
> > > 
> > > https://forums.parallax.com/discussion/161967/minsky-circle-algorithm-generate-sin-and-cos-fast-with-only-add-sub-and-shift
> > > 
> > > Regards,
> > > Rudy Wieser
> > 
> > I said regular polygon that one is "much" worse then my approach?
> 
> 
> What i want is x dots evenly spaced around circle cordinates.

I think i got an idea now.

[toc] | [prev] | [next] | [standalone]


#38028

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-11 05:09 -0800
Message-ID<4ca72c7b-2cfd-4310-82d3-0fa8a917d889@googlegroups.com>
In reply to#38012
Den torsdag 9 januari 2020 kl. 09:20:03 UTC+1 skrev R.Wieser:
> Jonas,
> 
> Your google-fu is weak.
> 
> A quick search came up with this approach:
> 
> https://forums.parallax.com/discussion/161967/minsky-circle-algorithm-generate-sin-and-cos-fast-with-only-add-sub-and-shift
> 
> Regards,
> Rudy Wieser

This is what i try to do but without use of cosinus and sinus.

function circleNodes(nodes) {
    var step = 2 * Math.PI / (nodes);
    var h = 670;
    var k = 370;
    var r = 320;
    theta = 0;
    for (var m = 0; m < arr.length; m++) {
        nodeout = printnode[m];
        var MX = h + r * Math.cos(theta);
        var MY = k - r * Math.sin(theta);
        arr[nodeout].rposX = Math.round(MX);
        arr[nodeout].rposY = Math.round(MY);
        theta += step

    }
}

[toc] | [prev] | [next] | [standalone]


#38033

FromJulio Di Egidio <julio@diegidio.name>
Date2020-01-11 22:13 -0800
Message-ID<9c881808-2ee9-42c0-b964-b4f33f586b8a@googlegroups.com>
In reply to#38028
On Saturday, 11 January 2020 14:09:55 UTC+1, Jonas Thörnvall  wrote:

> This is what i try to do but without use of cosinus and sinus.

LOL.  To begin with, my congratulations for some reasonably readable
code.  That said, and nitpicking aside (e.g. on the still too prominent
use of globals, as well as probably some plain forgetting of the var
keyword on some locals), I'd raise a couple of main points:

Firstly, I think it's messed up with nodes vs arr: on the first line
you are using nodes as a count, later you count m up to arr.length,
where arr I must guess is a global: this is too messy, either one or
the other.  Indeed, to make it clean, I would suggest that all you
actually need is a function that gets a count in input, the count of
distinct points, and returns a brand new array with the needed
coordinates, whose length of course will be that count.  (And,
printnode looks like an indirect index: but your function shouldn't
be bothered with "external" considerations, i.e. just build and
return an array and rather do that on the caller's side if you
really need.  At least, from what I can gather...)  (Plus, those
radius and offsets better be defined in some outer scope, even be
globals if nothing else, anyway not hard-coded into the function.)

Secondly, that logic of calculating the single step, then adding it
repeatedly, plain accumulates floating-point errors, so it is not the
way to write such a loop.  What you rather do is:

  var sect = 2 * Math.PI / count;
  for (var m = 0; m < count; ++m) {
    var theta = sect * m;
  ...

You still get a minimum of floating-point error there, but it will
not accumulate.

Also, you do not usually need to round off coordinates, certainly
not for drawing.  If you thought pixel coordinates on a canvas are
integers, note that they aren't...

> function circleNodes(nodes) {
>     var step = 2 * Math.PI / (nodes);
>     var h = 670;
>     var k = 370;
>     var r = 320;
>     theta = 0;
>     for (var m = 0; m < arr.length; m++) {
>         nodeout = printnode[m];
>         var MX = h + r * Math.cos(theta);
>         var MY = k - r * Math.sin(theta);
>         arr[nodeout].rposX = Math.round(MX);
>         arr[nodeout].rposY = Math.round(MY);
>         theta += step
>     }
> }

I could have rewritten it with my suggestions incorporated, it's few
lines of code, but I wouldn't want to spoil the exercise...

HTH,

Julio

[toc] | [prev] | [next] | [standalone]


#38054

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-13 09:58 -0800
Message-ID<faff8a41-a0a7-4fb3-8a91-46b0f9801324@googlegroups.com>
In reply to#38033
Den söndag 12 januari 2020 kl. 07:13:13 UTC+1 skrev Julio Di Egidio:
> On Saturday, 11 January 2020 14:09:55 UTC+1, Jonas Thörnvall  wrote:
> 
> > This is what i try to do but without use of cosinus and sinus.
> 
> LOL.  To begin with, my congratulations for some reasonably readable
> code.  That said, and nitpicking aside (e.g. on the still too prominent
> use of globals, as well as probably some plain forgetting of the var
> keyword on some locals), I'd raise a couple of main points:
> 
> Firstly, I think it's messed up with nodes vs arr: on the first line
> you are using nodes as a count, later you count m up to arr.length,
> where arr I must guess is a global: this is too messy, either one or
> the other.  Indeed, to make it clean, I would suggest that all you
> actually need is a function that gets a count in input, the count of
> distinct points, and returns a brand new array with the needed
> coordinates, whose length of course will be that count.  (And,
> printnode looks like an indirect index: but your function shouldn't
> be bothered with "external" considerations, i.e. just build and
> return an array and rather do that on the caller's side if you
> really need.  At least, from what I can gather...)  (Plus, those
> radius and offsets better be defined in some outer scope, even be
> globals if nothing else, anyway not hard-coded into the function.)
> 
> Secondly, that logic of calculating the single step, then adding it
> repeatedly, plain accumulates floating-point errors, so it is not the
> way to write such a loop.  What you rather do is:
> 
>   var sect = 2 * Math.PI / count;
>   for (var m = 0; m < count; ++m) {
>     var theta = sect * m;
>   ...
> 
> You still get a minimum of floating-point error there, but it will
> not accumulate.
> 
> Also, you do not usually need to round off coordinates, certainly
> not for drawing.  If you thought pixel coordinates on a canvas are
> integers, note that they aren't...
> 
> > function circleNodes(nodes) {
> >     var step = 2 * Math.PI / (nodes);
> >     var h = 670;
> >     var k = 370;
> >     var r = 320;
> >     theta = 0;
> >     for (var m = 0; m < arr.length; m++) {
> >         nodeout = printnode[m];
> >         var MX = h + r * Math.cos(theta);
> >         var MY = k - r * Math.sin(theta);
> >         arr[nodeout].rposX = Math.round(MX);
> >         arr[nodeout].rposY = Math.round(MY);
> >         theta += step
> >     }
> > }
> 
> I could have rewritten it with my suggestions incorporated, it's few
> lines of code, but I wouldn't want to spoil the exercise...
> 
> HTH,
> 
> Julio

Well the code is from an old graph project i had laying around, i did not clean it up removing the arrays that store the nodes.

But i think you missed the question completly?
How to do it withoug use of angles "cosin sinus".

Here it is without but, but i have tried to think about howto actually get x "nodes/vertices" evenly spaced without use "sin cosin" using just algebra or geometry. And i "think" it will use an equation system with two equation to solve the cordinates around the arcs.

So can you do it, i sure i could some years ago, but i am not that sure any longer.
 

[toc] | [prev] | [next] | [standalone]


#38057

FromJulio Di Egidio <julio@diegidio.name>
Date2020-01-13 10:16 -0800
Message-ID<4988cad9-7482-418b-be1e-672e0fef3d34@googlegroups.com>
In reply to#38054
On Monday, 13 January 2020 18:58:36 UTC+1, Jonas Thörnvall  wrote:

> But i think you missed the question completly?

I just meant do not even think about it.

> How to do it withoug use of angles "cosin sinus".

You don't, that's how you calculate those coordinates, with or without
an explicit system of equations.  What you could do, but mind you that
it is utterly disproportionate when not outright inefficient for common
needs, is pre-compute tables of sines and cosines in order to reduce the
calculations at run-time to some table lookups, with the size of the
tables proportional to the level of precision needed.

Julio

[toc] | [prev] | [next] | [standalone]


#38058

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-13 11:16 -0800
Message-ID<4bbf8176-25c9-43f5-abc9-e19697238b13@googlegroups.com>
In reply to#38057
Den måndag 13 januari 2020 kl. 19:16:34 UTC+1 skrev Julio Di Egidio:
> On Monday, 13 January 2020 18:58:36 UTC+1, Jonas Thörnvall  wrote:
> 
> > But i think you missed the question completly?
> 
> I just meant do not even think about it.
> 
> > How to do it withoug use of angles "cosin sinus".
> 
> You don't, that's how you calculate those coordinates, with or without
> an explicit system of equations.  What you could do, but mind you that
> it is utterly disproportionate when not outright inefficient for common
> needs, is pre-compute tables of sines and cosines in order to reduce the
> calculations at run-time to some table lookups, with the size of the
> tables proportional to the level of precision needed.
> 
> Julio

Well, a greater mind then yours probably could bring down the computational complexity.

[toc] | [prev] | [next] | [standalone]


#38061

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2020-01-13 12:22 -0800
Message-ID<aff0763d-eff8-46f2-ba60-0bcea3dcf5b0@googlegroups.com>
In reply to#38054
On Monday, January 13, 2020 at 11:58:36 AM UTC-6, Jonas Thörnvall wrote:

> But i think you missed the question completly?
> How to do it withoug use of angles "cosin sinus".
> 
> Here it is without but, but i have tried to think about howto actually get x "nodes/vertices" evenly spaced without use "sin cosin" using just algebra or geometry. And i "think" it will use an equation system with two equation to solve the cordinates around the arcs.
> 
> So can you do it, i sure i could some years ago, but i am not that sure any longer.


In elementary/primary school you should have learned the equation for a circle:

x^2 + y^2 = r^2

Alternate form to solve for y:

y = +/- sqrt(r^2 - x^2)

That's enough for you to plot your points.

Assuming a circle of radius 5 and centered at the origin (0,0)

iterate x from -r to +r and solve for y:

y = +/- sqrt((5)^2 - (-5)^2)
  = +/- sqrt(25 - 25)
  = +/- sqrt(0)
  = +/- 0
  = 0
y = +/- sqrt((5)^2 - (-4)^2)
  = +/- sqrt(25 - 16)
  = +/- sqrt(9)
  = +/- 3
y = +/- sqrt((5)^2 - (-3)^2)
  = +/- sqrt(25 - 9) 
  = +/- sqrt(16) 
  = +/- 4
...

now convert that to JavaScript

[toc] | [prev] | [next] | [standalone]


#38064

FromJulio Di Egidio <julio@diegidio.name>
Date2020-01-13 12:53 -0800
Message-ID<5b68590a-f539-4a21-ac6c-b7a2d4824524@googlegroups.com>
In reply to#38061
On Monday, 13 January 2020 21:22:25 UTC+1, Michael Haufe (TNO)  wrote:

> y = +/- sqrt(r^2 - x^2)
> 
> That's enough for you to plot your points.

That is just not enough to find the point at 2pi/n degrees from the
horizontal.  And it is quite a naive approach even for just drawing a
circle properly.

When Dunning-Kruger becomes a compliment.

*Plonk*

Julio

[toc] | [prev] | [next] | [standalone]


#38067

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2020-01-13 15:31 -0800
Message-ID<8657291f-d1f2-4eb5-b590-e5f77aa1653b@googlegroups.com>
In reply to#38064
On Monday, January 13, 2020 at 2:53:19 PM UTC-6, Julio Di Egidio wrote:
> On Monday, 13 January 2020 21:22:25 UTC+1, Michael Haufe (TNO)  wrote:
> 
> > y = +/- sqrt(r^2 - x^2)
> > 
> > That's enough for you to plot your points.
> 
> That is just not enough to find the point at 2pi/n degrees from the
> horizontal.  And it is quite a naive approach even for just drawing a
> circle properly.

Which is why you need to read the rest of the post...

it's obviously not what should be done by the OP but he insists on avoiding more direct approaches

> When Dunning-Kruger becomes a compliment.
> 
> *Plonk*

The pot calling the kettle black...

[toc] | [prev] | [next] | [standalone]


#38072

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-13 16:03 -0800
Message-ID<547d2d31-2b48-41ff-93f2-b39e2644fffa@googlegroups.com>
In reply to#38064
Den måndag 13 januari 2020 kl. 21:53:19 UTC+1 skrev Julio Di Egidio:
> On Monday, 13 January 2020 21:22:25 UTC+1, Michael Haufe (TNO)  wrote:
> 
> > y = +/- sqrt(r^2 - x^2)
> > 
> > That's enough for you to plot your points.
> 
> That is just not enough to find the point at 2pi/n degrees from the
> horizontal.  And it is quite a naive approach even for just drawing a
> circle properly.
> 
> When Dunning-Kruger becomes a compliment.
> 
> *Plonk*
> 
> Julio

The solution is here.
http://zonalandeducation.com/mmts/intersections/intersectionLineAndParabolla1/intersectionLineParabolla1.html

But is lt slower or faster?

[toc] | [prev] | [next] | [standalone]


#38065

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-13 13:00 -0800
Message-ID<3483a8a7-d454-47bc-a0f9-e5830211e5a8@googlegroups.com>
In reply to#38061
Den måndag 13 januari 2020 kl. 21:22:25 UTC+1 skrev Michael Haufe (TNO):
> On Monday, January 13, 2020 at 11:58:36 AM UTC-6, Jonas Thörnvall wrote:
> 
> > But i think you missed the question completly?
> > How to do it withoug use of angles "cosin sinus".
> > 
> > Here it is without but, but i have tried to think about howto actually get x "nodes/vertices" evenly spaced without use "sin cosin" using just algebra or geometry. And i "think" it will use an equation system with two equation to solve the cordinates around the arcs.
> > 
> > So can you do it, i sure i could some years ago, but i am not that sure any longer.
> 
> 
> In elementary/primary school you should have learned the equation for a circle:
> 
> x^2 + y^2 = r^2
> 
> Alternate form to solve for y:
> 
> y = +/- sqrt(r^2 - x^2)
> 
> That's enough for you to plot your points.
> 
> Assuming a circle of radius 5 and centered at the origin (0,0)
> 
> iterate x from -r to +r and solve for y:
> 
> y = +/- sqrt((5)^2 - (-5)^2)
>   = +/- sqrt(25 - 25)
>   = +/- sqrt(0)
>   = +/- 0
>   = 0
> y = +/- sqrt((5)^2 - (-4)^2)
>   = +/- sqrt(25 - 16)
>   = +/- sqrt(9)
>   = +/- 3
> y = +/- sqrt((5)^2 - (-3)^2)
>   = +/- sqrt(25 - 9) 
>   = +/- sqrt(16) 
>   = +/- 4
> ...
> 
> now convert that to JavaScript

Yeah... that was basically what i did, in first post but i wanted the points/cordinatepairs evenly spaced to print a polygon 3 sided 4 sided 5,6,7,8 and so on.

Are you telling me that what you do above do that?

[toc] | [prev] | [next] | [standalone]


#38068

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2020-01-13 15:35 -0800
Message-ID<896b6cfa-9412-4910-9e5d-3c153a0fb72a@googlegroups.com>
In reply to#38065
On Monday, January 13, 2020 at 3:00:40 PM UTC-6, Jonas Thörnvall wrote:
> 
> Yeah... that was basically what i did, in first post but i wanted the points/cordinatepairs evenly spaced to print a polygon 3 sided 4 sided 5,6,7,8 and so on.
> 
> Are you telling me that what you do above do that?

If you want other polygons you could just follow my example here:

<https://codepen.io/mlhaufe/pen/kkXWmx>

It does use sine and cosine though...

[toc] | [prev] | [next] | [standalone]


#38069

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-13 15:43 -0800
Message-ID<d92f10f3-3574-4c8e-8188-68c013e32929@googlegroups.com>
In reply to#38068
Den tisdag 14 januari 2020 kl. 00:35:44 UTC+1 skrev Michael Haufe (TNO):
> On Monday, January 13, 2020 at 3:00:40 PM UTC-6, Jonas Thörnvall wrote:
> > 
> > Yeah... that was basically what i did, in first post but i wanted the points/cordinatepairs evenly spaced to print a polygon 3 sided 4 sided 5,6,7,8 and so on.
> > 
> > Are you telling me that what you do above do that?
> 
> If you want other polygons you could just follow my example here:
> 
> <https://codepen.io/mlhaufe/pen/kkXWmx>
> 
> It does use sine and cosine though...

I do not think your polygons regular, but i already have regular polygons using cosine and sine.

[toc] | [prev] | [next] | [standalone]


#38071

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2020-01-14 01:01 +0100
Message-ID<7744077.NyiUUSuA9g@PointedEars.de>
In reply to#38061
Michael Haufe (TNO) wrote:

> In elementary/primary school you should have learned the equation for a
> circle:
> 
> x^2 + y^2 = r^2
> 
> Alternate form to solve for y:
> 
> y = +/- sqrt(r^2 - x^2)
> 
> That's enough for you to plot your points.
> 
> Assuming a circle of radius 5 and centered at the origin (0,0)
> 
> iterate x from -r to +r and solve for y:
> 
> y = +/- sqrt((5)^2 - (-5)^2)
>   = +/- sqrt(25 - 25)
>   = +/- sqrt(0)
>   = +/- 0
>   = 0
> y = +/- sqrt((5)^2 - (-4)^2)
>   = +/- sqrt(25 - 16)
>   = +/- sqrt(9)
>   = +/- 3
> y = +/- sqrt((5)^2 - (-3)^2)
>   = +/- sqrt(25 - 9)
>   = +/- sqrt(16)
>   = +/- 4
> ...
> 
> now convert that to JavaScript

let r = 5;
document.body.appendChild(
  (new DOMParser()).parseFromString(
    `<svg xmlns="http://www.w3.org/2000/svg" width="${2 * r + 2}" 
height="${2 * r + 2}" stroke="black"><circle cx="${r + 1}" cy="${r + 1}" 
r="${r}" 
fill="transparent"/></svg>`,
    'image/svg+xml'
  ).documentElement
);

;-b

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#38073

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-13 16:07 -0800
Message-ID<d6935f2f-963e-4180-9853-740b5ee144ac@googlegroups.com>
In reply to#38071
Den tisdag 14 januari 2020 kl. 01:01:41 UTC+1 skrev Thomas 'PointedEars' Lahn:
> Michael Haufe (TNO) wrote:
> 
> > In elementary/primary school you should have learned the equation for a
> > circle:
> > 
> > x^2 + y^2 = r^2
> > 
> > Alternate form to solve for y:
> > 
> > y = +/- sqrt(r^2 - x^2)
> > 
> > That's enough for you to plot your points.
> > 
> > Assuming a circle of radius 5 and centered at the origin (0,0)
> > 
> > iterate x from -r to +r and solve for y:
> > 
> > y = +/- sqrt((5)^2 - (-5)^2)
> >   = +/- sqrt(25 - 25)
> >   = +/- sqrt(0)
> >   = +/- 0
> >   = 0
> > y = +/- sqrt((5)^2 - (-4)^2)
> >   = +/- sqrt(25 - 16)
> >   = +/- sqrt(9)
> >   = +/- 3
> > y = +/- sqrt((5)^2 - (-3)^2)
> >   = +/- sqrt(25 - 9)
> >   = +/- sqrt(16)
> >   = +/- 4
> > ...
> > 
> > now convert that to JavaScript
> 
> let r = 5;
> document.body.appendChild(
>   (new DOMParser()).parseFromString(
>     `<svg xmlns="http://www.w3.org/2000/svg" width="${2 * r + 2}" 
> height="${2 * r + 2}" stroke="black"><circle cx="${r + 1}" cy="${r + 1}" 
> r="${r}" 
> fill="transparent"/></svg>`,
>     'image/svg+xml'
>   ).documentElement
> );
> 
> ;-b
> 
> -- 
> PointedEars
> FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Well the solution is here once you realise you can a quadrant into sectors of equal length using a diagonal. then it is just finding the intersection but is it faster or slower.

http://zonalandeducation.com/mmts/intersections/intersectionLineAndParabolla1/intersectionLineParabolla1.html

[toc] | [prev] | [next] | [standalone]


#38055

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2020-01-13 10:01 -0800
Message-ID<23eb3622-4837-4800-81d9-e092ef376b04@googlegroups.com>
In reply to#38033
Den söndag 12 januari 2020 kl. 07:13:13 UTC+1 skrev Julio Di Egidio:
> On Saturday, 11 January 2020 14:09:55 UTC+1, Jonas Thörnvall  wrote:
> 
> > This is what i try to do but without use of cosinus and sinus.
> 
> LOL.  To begin with, my congratulations for some reasonably readable
> code.  That said, and nitpicking aside (e.g. on the still too prominent
> use of globals, as well as probably some plain forgetting of the var
> keyword on some locals), I'd raise a couple of main points:
> 
> Firstly, I think it's messed up with nodes vs arr: on the first line
> you are using nodes as a count, later you count m up to arr.length,
> where arr I must guess is a global: this is too messy, either one or
> the other.  Indeed, to make it clean, I would suggest that all you
> actually need is a function that gets a count in input, the count of
> distinct points, and returns a brand new array with the needed
> coordinates, whose length of course will be that count.  (And,
> printnode looks like an indirect index: but your function shouldn't
> be bothered with "external" considerations, i.e. just build and
> return an array and rather do that on the caller's side if you
> really need.  At least, from what I can gather...)  (Plus, those
> radius and offsets better be defined in some outer scope, even be
> globals if nothing else, anyway not hard-coded into the function.)
> 
> Secondly, that logic of calculating the single step, then adding it
> repeatedly, plain accumulates floating-point errors, so it is not the
> way to write such a loop.  What you rather do is:
> 
>   var sect = 2 * Math.PI / count;
>   for (var m = 0; m < count; ++m) {
>     var theta = sect * m;
>   ...
> 
> You still get a minimum of floating-point error there, but it will
> not accumulate.
> 
> Also, you do not usually need to round off coordinates, certainly
> not for drawing.  If you thought pixel coordinates on a canvas are
> integers, note that they aren't...
> 
> > function circleNodes(nodes) {
> >     var step = 2 * Math.PI / (nodes);
> >     var h = 670;
> >     var k = 370;
> >     var r = 320;
> >     theta = 0;
> >     for (var m = 0; m < arr.length; m++) {
> >         nodeout = printnode[m];
> >         var MX = h + r * Math.cos(theta);
> >         var MY = k - r * Math.sin(theta);
> >         arr[nodeout].rposX = Math.round(MX);
> >         arr[nodeout].rposY = Math.round(MY);
> >         theta += step
> >     }
> > }
> 
> I could have rewritten it with my suggestions incorporated, it's few
> lines of code, but I wouldn't want to spoil the exercise...
> 
> HTH,
> 
> Julio

By the way what do you mean by that the X, Y cordinates not limited to the integer cordinate space. Can computers move pixels slightly by diffuse aspects of them????? I mean the number of pixels is fixed so "WHY" isn't the pixels integer cordinates?

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web