Showing posts with label Bezier curve. Show all posts
Showing posts with label Bezier curve. Show all posts

Sunday, June 13, 2021

Numworks: Bezier Curve (Using Ion Module)

 Numworks: Bezier Curve (Using Ion Module)


Introduction


The script bezier1 plots a Bezier curve of degree 1, 2, or 3 in one of four colors: black (0), red (1), green (2), and blue (3).  The script uses the ion module, which allows input directly from the keyboard.


The ion module function keydown returns True if a certain key has been pressed, otherwise, False is returned.  Keys are designed by the declaration KEY_(key name).  For example: 


KEY_ZERO:  0 key

KEY_ONE:  1 key

KEY_TWO:  2 key

KEY_THREE: 3 key

KEY_FOUR: 4 key

KEY_FIVE: 5 key

KEY_SIX:  6 key

KEY_SEVEN:  7 key

KEY_EIGHT:  8 key

KEY_NINE:  9 key


Numworks script:   bezier1.py


# Bezier Curve 

# 2021-05-02 EWS


from ion import *

from math import *

from matplotlib.pyplot import *


# functions


# key press

def key():

  while True:

    if keydown(KEY_ONE):

      return 1

    if keydown(KEY_TWO):

      return 2

    if keydown(KEY_THREE):

      return 3


# linear plot

def linbez(x0,y0,x1,y1):

  xlist=[x0]

  ylist=[y0]

  t=0

  while t<1:

    t+=0.05

    xlist.append(x0*(1-t)+x1*t)

    ylist.append(y0*(1-t)+y1*t)

  return [xlist,ylist]


# quadratic plot

def quadbez(x0,y0,x1,y1,x2,y2):

  xlist=[x0]

  ylist=[y0]

  t=0

  while t<1:

    t+=0.05

    x=x0*(1-t)**2+2*x1*t*(1-t)+x2*t**2

    y=y0*(1-t)**2+2*y1*t*(1-t)+y2*t**2

    xlist.append(x)

    ylist.append(y)

  return [xlist,ylist]


# cubic plot

def cubbez(x0,y0,x1,y1,x2,y2,x3,y3):

  xlist=[x0]

  ylist=[y0]

  t=0

  while t<1:

    t+=0.05

    x=x0*(1-t)**3+3*x1*t*(1-t)**2+3*x2*t**2*(1-t)+x3*t**3

    y=y0*(1-t)**3+3*y1*t*(1-t)**2+3*y2*t**2*(1-t)+y3*t**3

    xlist.append(x)

    ylist.append(y)

  return [xlist,ylist]  


# color selection

def colsel():

  print("Choose a color:")

  print("0: black, 1: red \n2: green, 3: blue")

  while True:

    if keydown(KEY_ZERO):

      return 'black'

    if keydown(KEY_ONE):

      return 'red'

    if keydown(KEY_TWO):

      return 'green'

    if keydown(KEY_THREE):

      return 'blue'

      

# main program

print("Bezier Curve Plot")

print("Order? Press 1, 2, or 3.")

n=key()

x0=float(input("x0? "))

y0=float(input("y0? "))

x1=float(input("x1? "))

y1=float(input("y1? "))

if n==1:

  clist=linbez(x0,y0,x1,y1)

if n>1:

  x2=float(input("x2? "))

  y2=float(input("y2? "))

if n==2:

  clist=quadbez(x0,y0,x1,y1,x2,y2)

if n==3:

  x3=float(input("x3? "))

  y3=float(input("y3? "))

  clist=cubbez(x0,y0,x1,y1,x2,y2,x3,y3)

# color section

ch=colsel()

# plotting

xlist=clist[0]

ylist=clist[1]

xa=min(xlist)-1.5

xb=max(xlist)+1.5

ya=min(ylist)-1.5

yb=max(ylist)+1.5

# set axis and turn it on

axis((xa,xb,ya,yb))

axis("on")

# plot the curve

plot(xlist,ylist,color=ch)

show()


Program Size: 1995 bytes 


Eddie

All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 

Wednesday, February 10, 2016

TI-84 Plus: Bézier Curves

TI-84 Plus:  Bézier Curves

The Bézier Curve is defined as a set of parametric equations of n points:

x(t) = ∑( nCr(n,r) * (1-t)^(n-k) * t^k * x_k ) from k = 0 to n

y(t) = ∑( nCr(n,r) * (1-t)^(n-k) * t^k * y_k ) from k = 0 to n

For 0 ≤ t ≤ 1

The curve fits points (x_0, y_0) (when t = 0) and (x_n, y_n) (when t = 1).   The degree of the polynomial is n-1.

This blog covers the TI-84 Plus.  I have a version for the TI nSpire and HP 50g which can be found here:


The arguments given for the BEZIER program are two lists: one of x-coordinates and one for y-coordinates.  The program switches to Parametric mode.

In order to accomplish this, I store lists to equation variables, convert them to strings, and build a string to be converted to the equation. 

The commands Equ>String and String>Equ are found in the catalog ([2nd] [ 0 ]).  The DelVar command can handle one variable at a time.  Temporary variables (XT5, YT5, XT6, YT6, Str7, Str8, Str9, Str0) are deleted at the end of the program to save space.



Program BEZIER

Disp "BEZIER CURVE"
Input "X=",L₁
Input "Y=",L₂
If dim(L₁)≠dim(L₂)
Then
Disp "LISTS NOT SAME LENGTH"
Stop
End
Param
dim(L₁)→N
seq(K,K,0,N-1)→L₅
seq(N-K-1,K,0,N-1)→L₆
seq((N-1) nCr K,K,0,N-1)→L₄
L₄*L₁→L₃
L₄*L₂→L₄
"L₃"→X₅T:Equ>String(X₅T,Str7)
"L₄"→Y₅T:Equ>String(Y₅T,Str8)
"L₅"→X₆T:Equ>String(X₆T,Str9)
"L₆"→Y₆T:Equ>String(Y₆T,Str0)
"sum("+Str7+"*(1-T)^"+Str0+"*T^"+Str9+")"→X₁T
"sum("+Str8+"*(1-T)^"+Str0+"*T^"+Str9+")"→Y₁T
0→Tmin
1→Tmax
.05→Tstep
DelVar X₅T:DelVar Y₅T
DelVar X₆T:DelVar Y₆T
DelVar Str7:DelVar Str8
DelVar Str9:DelVar Str0
ZoomFit:Zoom Out
Examples:

Example 1:  (0,0), (1,2), (3,1)
X List: {0,1,3}
Y List: {0,2,1}





Example 2:  (-2,4), (0,0), (2,4), (4,2)
X List: {-2, 0, 2, 4}
Y List: {4, 0, 4, 2}


Example 3: (0, 10), (1, 6), (3, 3), (5, 8)
X List: {0, 1, 3, 5}
Y List: {10, 6, 3, 8}





This blog is property of Edward Shore.  2016

Thursday, February 26, 2015

Throwback Thursday: Programs for the HP 49g+/50g from 2003-2007

Throwback Thursday:  Programs for the HP 49g+/50g from 2003-2007

My first Hewlett Packard calculator was an HP 48G+, which I bought in 2000.  I really did not get serious into RPN and RPL until I bought an HP 49g+ in 2003.  Despite keyboard problems, the 49g+ was my go-to calculator.   The first 49g+ had a plus key that came loose, the second one I had trouble with the ON button.  Thankfully, by the time the HP 50g was released, the keyboard was reliable.  I bought my first 50g I think in 2003 – not sure.  It was the standard black 50g with orange and white text. Several years later, I ordered the blue 50g from Spain.  Recently, I traded the black 50g with a friend of mine (me getting a HP 42S in return). 

During 2003 to 2006 (I wish I noted the dates when I programmed these things back then), I made a library of programs that covered math, science, finance, and stats.  After finding them still on the SD card, today, I am going to list some of my favorite programs from that era. 

All the programs presented were done in User RPL and most of them, if not all of them, should be able to be used on any of the HP 48 series.  (no guarantees)

QCKPLOT – Simplified Plot Screen
(updated 2/19/2015)

QCKPLOT is probably my favorite out of the bunch.  QCKPLOT allows the user to plot a single function y(x) in a familiar graph set up screen input interface.  The plotting interface with the 49g+ and 50g took a while to get used to. 



QCKPLOT:
<< ‘X’ PURGE
“Quick Function Plot” 
{ { “y(x)=” “Enter y(x)” }
{ “xmin=” “x minimum” }
{ “xmax=” “x maximum” }
{ “ymin=” “y minimum” }
{ “ymax=” “y maximum” } }
{ 2 2 } { } { } INFORM
IF 0 == THEN KILL END
EVAL YRNG XRNG STEQ ‘X’ INDEP FUNCTION (0,0) AXES
ERASE DRAX DRAW PICT {#0d #0d} “Y=” EQ →STR + 1
→GROB GOR PICTURE >>

You can use RPN to build expressions.  However, everything for the window variables must evaluate to numbers.  For example, if you want to set xmax to 2π, use 2 π * →NUM [ENTER].  Which brings me to my next favorite…

PCTXT:  Puts text on the graph screen.

PCTXT inserts a text screen to any Cartesian point on the graph screen.  The program does not immediately show the graph screen upon completion, which allows for use as a subroutine.  If PCTXT is used a standalone program, press the left arrow button [ ← ] or hold [left shift] while pressing [ F3 ] to see the results. (I am assuming you are in RPN mode).

Input:
2:  coordinates
1:  string

PCTXT:
<< 1 →GROB PICT ROT C→PX ROT GXOR >>

ZETA41:  Zeta Function Approximation

The HP 49g+/50g does not have a built in Reimann Zeta function.  Using the standard definition of the zeta function takes forever.

Σ(I=1, infinity, 1/X^Z)

 What is presented here is a series that converges faster than the standard definition. The upper limit of 250 is arbitrary – you can increase the upper limit for better accuracy, but keep in mind it there is a cost in calculation time. The accuracy increases as X increases.

Input:
1:  X

ZETA41:
<< → X ‘∑(I=1,250,(-1)^(I+1)/I^X)/(1-2^(1-X))’ >>

FIB:  nth Fibonacci Number

FIB uses an explicit formula to calculate the nth Fibonacci number, rather than use a sequence loop. 

Input:
1:  N

FIB:
<< → N
<< 1 5 √ + 2 / N ^ 1 5 √ - 2 / N ^
- 5 √ / EVAL 0 RND >> >>

Test:  FIB(8) = 21,  FIB(12) = 144

CRDTRN Subroutine:  (Coordinate Transformation)

CRDTRN transforms a coordinate (x, y) to a new coordinate (x’, y’) with new center (xc, yc) and rotation angle θ.  CRDTRN is both a good standalone program and a useful subroutine.

Input:
5:  XOLD
4:  YOLD
3:  XCTR
2:  YCTR
1:  θ

CRDTRN
<< → XOLD YOLD XCTR YCTR θ
<< XOLD YOLD R→C XCTR YCTR R→C – θ DUP COS →NUM SWAP NEG SIN →NUM R→C * >> >>

Example:  Let the original coordinate be (1,2)  What would be the coordinates if the center was moved to (-1, 3) and the plane was rotated 60⁰ (π/3 radians)?

In Raidian Mode:
5:  1
4:  2
3:  -1
2:  3
1:  π/3

New coordinate:  (.13397456215, -2.23205080757)

QUADRES:  Quadratic Regression (uses ∑DAT)
(Update 2/19/2015)

QUADRES aims to fulfill a missing statistical regression for the HP 49g+/50g series:  quadratic regression.

Y = a*X^2 + b*X + c

The program uses the standard system statistics matrix ΣDAT.  The coefficients are returned in a matrix:

[[ c ]
[ b ]
[ a ]]

Example:

ΣDAT =
[[ 2, .23 ]
[ 3, .58 ]
[ 4, .96 ]
[ 8 1.85 ]
[ 16 3.66 ]]

Results:
[[ -.302930140195 ]
[ .304114954514 ]
[ -3.55628308881E-3 ]]

Or Y = -3.55628308881E-3*X^2 + .304114954514*X - .302930140195

QUADRES:
<< ∑DAT 1 COL- VANDERMONDE DUP SIZE OBJ→ DROP DROP 3 2 →LIST {1,1}
SWAP SUB LSQ >>

CSUM:  Sum of each of the matrix’s columns
RSUM:  Sum of each of the matrix’s rows

An example for CSUM and RSUM:

Matrix:
[[ 2, 3, -7 ]
[ 1, 8, 1 ]
[ -3, 4, 6 ]]

CSUM: [0, 15, 0]
RSUM: [-2, 10, 7]

Input:  Matrix

CSUM:
<< →COL → S
<< 1 S START AXL ∑LIST S ROLL NEXT
S →LIST AXL >> >>

RSUM:
<< →ROW → S
<< 1 S START AXL ∑LIST S ROLL NEXT
S →LIST AXL >> >>

LVEVAL:  Evaluate (simplify) every element in a list or vector

LVEVAL evaluates and simplifies each component of a given list or vector.  For example, LVEVAL turns a list consisting of:

{‘e^2’, ‘π/2’, ‘5.273*LOG(3.44)’} to:

In exact mode:
{‘e^2’, ‘π/2’, 2.82927266768}

Or in approximate mode:
{7.38905609893, 1.5707963268, 2.82927266768}

Input:
1:  list/vector

LVEVAL:
<< → LV
<< LV TYPE 5 IF ≠
THEN LV AXL ‘LV’ STO 1 SF END 1 LV SIZE
FOR I LV I GET EVAL LV I 3 ROLL PUT ‘LV’
STO NEXT IF 1 FS? THEN LV AXL ‘LV’ STO 1
CF END LV >> >>



Bezier Curve

This program draws a Bezier curve.  I honestly don’t quite remember what α0, β0, α1, and β1 represent – I think they are points that are not on the curve be affect the path of the curve. (X0, Y0) and (X1, Y1) are end points. 


« 'T' PURGE PARAMETRIC { T 0. 1. } INDEP "Bézier Curve" { "X0" "Y0" "α0" "ß0" "X1" "Y1" "α1" "ß1" } { 2. 4. } { } { } INFORM 0.
  IF ==
  THEN KILL
  END OBJ→ DROP → X0 Y0 α0 ß0 X1 Y1 α1 ß1
  « X0 X1 - 2. * α0 α1 + 3. * + 'T' 3. ^ * X1 X0 - 3. * α1 α0 2. * + 3. * - 'T' 2. ^ * + α0 3. * 'T' * + X0 + Y0 Y1 - 2. * ß0 ß1 + 3. * + 'T' 3. ^ * Y1 Y0 - 3. * ß1 ß0 2. * + 3. * - 'T' 2. ^ * + ß0 3. * 'T' * + Y0 + i * + 'XY1(T)' SWAP = DEFINE 'T' XY1 STEQ 'T' XY1 ERASE AUTO DRAX DRAW PICTURE  »  »


One Minute Marvels

The document “One Minute Marvels” by Richard Nelson and Wlodek Mier-Jedrzejowicz is a collection of short, very effective RPL programs for the HP 48 series.  Programs include list operations (including rotate a list’s elements, fill a list with zeroes, randomize a list), calendar functions (including day of the week, stopwatch programs), and math and utility programs (LCD, GCM, Ulad Principle, quick quadratic equation, and flag toggles just to name a few).  For owners of the HP 48 series, including HP 49g+/50g, I consider this document an excellent addition to your mathematics library.

Link to download “One Minute Marvels”:  http://www.hpcalc.org/details.php?id=1691


Cheers to the HP 48 series and my trusty blue HP 50g!

Eddie



This blog is property of Edward Shore – 2015.

Symmetric Quartic Equation - Python

Symmetric Quartic Equation - Python Note: The code is programmed with a TI-84 Evo. No modules are used, so the scripts should be adopt...