Coding - what are polar coordinates? Sin/Cos, etc.

DonNews #10
------------------------------------------
Disabler/DPL                              
                                          
           Polar coordinates            
                                          
     (Everything you don’t know yet, or      
             afraid to know!)              
                                          
                                          
                l.Introduction                
                                          
  To begin with, let me tell you that if you
advanced coder, then maybe everything
the following may seem primitive and
meaningless, but this article is intended to
primarily for those new to demo building,
having some knowledge of geometry and
assembler.                               
  Sowhat are polar coordinates? This
one of the ways to represent a plane.
For simplicity, we will consider two-dimensional
plane. From geometry you (I hope)
you know that a point is in two dimensions
space has two coordinates - X and Y,
which are the offset from the start
coordinates But this is not always the case. So period
represented only in the Cartesian system
coordinates However, there isalso, how
at least one coordinate system is polar.
                                          
In the polar coordinate system, a point is given not by two coordinates, but by a long
vector on which the point (Q) is located and angle of rotation relative to the OX axis (P). And here the question arises: And on..I am this necessary? The point is this: for example, you need rotate point A with X,Y coordinates by angle equal to L. When using the Cartesian system coordinates you calculate: X' = X * cos L - Y * sin L Y' = X * sin L + Y * cos L where X` and Y` are new coordinates, but from the point programming point of view this fragment can be optimized: SL = sin L CL = cos L X' = X * CL - Y * SL Y' = X * SL + Y * CL here we save computation time by single calculation of sine and cosine, which can be calculated even on ordinary calculator (Made in Japan) by double multiplication, but that's a different topic articles. Yes, I almost forgot, it’s still possible calculate through the "Chebyshev polynomial" or like an endless series, but, as they say, in another time. So let's go back to our example. B same actions in the polar coordinate system can be done as follows: Q' = Q P' = P + L Well, is it easier? And this is just the beginning! For example, we have a square (four points) with center at the origin and we need increase it by some factor M andturn to angle L. In the Cartesian coordinate system: SL = sin L CL = cos L Xv = X + M; temporary temporary Yv = X + M X' = Xv * CL - Yv * SL Y' = Xv * SL + Yv * CL Do you feel the scale of the calculations? And now We take the polar system and get: Q' = Q + M P' = P + L Another question is brewing: If everything is so Simply, why doesn't everyone use it? polar coordinate system? And the thing is, that many people use, but don’t know how this is called, and also what most complex objects are simpler represent in the Cartesian system and displaying on the screen is also easier in Cartesian system. But, as they say, not everything is so complicated, as it may seem, especially from the ASSM. I won't spend too much time talking and loading you and yourself too with a tedious derivation of formulas,a I’ll bring you the finished ones right away. To put there is a point along polar coordinates formula: X = Q * cos P Y = Q * sin P And if you need to reverse the Cartesian coordinates to polar - the formula will help: _______ P=/X*X+Y*Y Q=arctg(Y/X) I sincerely hope that you NEVER you will use these formulas, because their use is from assma very difficult, especially for conversion from Cartesian to polar coordinates. 2.Use in assemblage To begin with, we (not us, but you!) need be able to “formulate” a sine table, which will also be a table cosine. And here everyone will help us "favorite"Basic`48. Login and dial the following: l0 Let adr=32768 20 Let atplit=64 30 For x=-Pi then Pi Step Pi/l28 40 Let sinx=l28 + atplit * Sin x S0 Roque adr,sinx 60 Let adr=adr + l 70 Next x 80 Rand Usr lSл9: Ret: Save "sin" Code 32768,2S6 This program will form and coxpohut on disk file "sin", inside which lies one sine period with amplitude from -64 to +64 and raised relative to the OX axis by 128, i.e. real values will be from 64 to 192. If you don’t understand: why is this necessary? Let me explain - it’s important to work with symbols numbers are unreasonably difficult, but that’s what we need no need. In what follows we will need a table sine tables, i.e. 64 sine tables with amplitude from -1 to +1,..., from -64 to +64. The size of each table is 256 bytes. This necessary so that when loading "flat" hexadecimal address (address whose low byte is zero, for example: #6000, #7800, etc.) each The subtable was also located with exact address.So, there is a table, there is an assm. Remains code it all :). Load XAS and type something for output and rotation by a given angle of four points: ;---------------------------------------- ; Rotate by angle "angle" with variable P ; equal to constant (64) pointz equ 4; Number of points org #6000 sinus lcode "sin"; Loading the table ent; This is the beginning! ld hl,#4000; Clearing the screen ld d,h ld e,l inc e ld(hl),l ld ьс,6l44 ldir ld (hl),7l ld ьс,767 ldir call view; Output points sleep hog a; Wait for any button to be pressed in a,#fe; after which we fall out cpl; from here! and 3l jr z,sleep ret;Exit! view ld a,(angle) ld with,a ld b,pointz ; c = rotation angle ; b = number of points ld hl,table viewl push хс,hl ld a,(hl) add a,c ldh,sinus&h; take the high byte ; table addresses ld l,a ld e,(hl) ; select the first coordinate add a,64 ; increase "a" by 64, which is equivalent ; transition from sine to cosine, i.e. ; cos x = sin (x + pi/2), and in this ; case pi/2 = 64, or a quarter of the table ; sine ld l,a ld a,(hl) ld with,a ld a,e ; choose the second coordinate call pixel; put a dot pop hl, ьсinc hl ; move on to the next point djnz viewl ret ; point procedure, slow but small pixel call 8880 add a,a add a,a add a,a ld b,a ld a,#fe sub b ld(pixell+l),a pixell set 0,(hl) ret angle db 0;rotation angle (0..255) table db 0.64,l28,l92 ; table of angles "Q" ; ANGLEassm = (256 * ANGLEreal) / 360 And now a program that turns on a given angle and at the same time increases "square". File "sin2" - a set of tables sine (see above). ;---------------------------------------- ; Rotate by "angle" and ; scaling by a given factor pointz equ 4; Number of points org #6000 sinus lcode "sin2"; Loading the table ent; This is the beginning! ld hl,#4000; Clearing the screen ld d,h ld e,l inc e ld(hl),l ld ьс,6l44 ldir ld (hl),7l ld ьс,767ldir call view; Output points sleep hog a; Wait for any button to be pressed in a,#fe; after which we fall out cpl; from here! and 3l jr z,sleep ret;Exit! view ld a,(angle) ld with,a ld b,pointz ; c = rotation angle ; b = number of points ld hl,table viewl push хс,hl ld a,(hl) add a,c ld with,ainc hl ld a,(tagnif) add a,(hl) add a,sinus&h; take the high byte ld h,a; table addresses ld l,s ld e,(hl) ; select the first coordinate ld a,s add a,64 ; increase "a" by 64, which is equivalent ; transition from sine to cosine, i.e. ; cos x = sin (x + pi/2), and in this ; case pi/2 = 64, or a quarter of the table ; sine ld l,a ld a,(hl) ld с,a ld a,e ; choose the second coordinate call pixel; put a dot pop hl, ьс inc hl inc hl ; move on to the next point djnz viewl ret ; point procedure, slow but small pixel call 8880 add a,a add a,a add a,a ld b,a ld a,#fe sub b ld(pixell+l),a pixell set 0,(hl) retangle db 0;rotation angle (0..255) tagnif db 0;scale (1..64) table db 0,l0,64,l0,l28,l0,l92,l0 ; table of angles "Q", and lengths of vectors "P" The following examples are displayed on the screen four dots and wait for any key to be pressed to exit. However, if waiting for a click replace the keys with the following, then get the effect of smooth rotation. ... loop halt call cls_pix call view ld hl,angle inc(hl) hog a in a,#fe cpl and 3l jr z,loop ... Where "cls_pix" is the procedure for erasing points That's probably all I could tell to you about such a terrible thing as the polar coordinate system. If you have any questions Write to the editorial office, we will answer everyone! In the next issue I will tell you o algorithms for fast calculation of sin functions (x), cos(x), as well as about algorithms fast multiplication, division and raising square.

Share your thoughts about the article