Showing posts with label polygons. Show all posts
Showing posts with label polygons. Show all posts

Sunday, August 4, 2024

TI-84 Plus CE Python: Drawing Shapes with the ti_plotlib module

 TI-84 Plus CE Python: Drawing Shapes with the ti_plotlib module


Introduction


Here are four scripts to draw shapes:


RECT8: rectangles and squares centered at (0, 0)

ELLIPSE8: ellipses and circles centered at (0, 0)

POLYGON8: polygons given the vertex points and number of vertices

INVFUNC8: draws a function f(x) and it’s inverse f^-1(x). Define the function is defined in the f(x) subroutine in the program.


The plot window is sized in sync with the TI-84’s screen size (320 pixels x 220 pixels) so that squares look like squares and circles look like circles. The window parameters are set as such:

Xmin = -16, Xmax = 16

Ymin = -10.5, Ymax = 10.5



TI-84 PLUS CE Python Script: RECT8.py





import ti_plotlib as plt

from math import *


# draw an rectangle using ti_plqtlib

# get parameters

print("Press [clear] to \nexit the graph.")

print("x:[-16,16] \ny:[-10.5,10.5]")

a=eval(input("horiz. length? "))

b=eval(input("vert. length? "))


# plot routine

plt.cls()

plt.title("Rectangle")

plt.window(-16,16,-10.5,10.5)

plt.axes("on")

plt.grid(1,1,"dot")


# color: blue

plt.color(0,0,192)


# pen size

plt.pen("medium","solid")


plt.line(-a/2,b/2,a/2,b/2,"")

plt.line(-a/2,-b/2,a/2,-b/2,"")

plt.line(-a/2,-b/2,-a/2,b/2,"")

plt.line(a/2,-b/2,a/2,b/2,"")


plt.show_plot()


TI-84 PLUS CE Python Script: ELLIPSE8.py





import ti_plotlib as plt

from math import *


# draw an ellipse using ti_plqtlib

# get parameters

print("Press [clear] to \nexit the graph.")

print("x:[-16,16] \ny:[-10.5,10.5]")

a=eval(input("x axis? "))

b=eval(input("y axis? "))


# plot routine

plt.cls()

plt.title("Ellipse")

plt.window(-16,16,-10.5,10.5)

plt.axes("on")

plt.pen("medium","solid")

plt.grid(1,1,"dot")


# color: green

plt.color(0,192,0)


for i in range(128):

  x=a*cos(i*pi/64)

  y=b*sin(i*pi/64)

  plt.plot(x,y,"o")


plt.show_plot()



TI-84 PLUS CE Python Script: POLYGON8.py





import ti_plotlib as plt

from math import *


# draw an rectangle using ti_plqtlib

# get parameters

print("Press [clear] to \nexit the graph.")

print("x:[-16,16] \ny:[-10.5,10.5]")


n=int(input("# of vertices? "))

a=eval(input("x1? "))

b=eval(input("y1? "))


x=[a]

y=[b]


for i in range(n-1):

  print("vertex ",i+2)

  c=eval(input("x? "))

  d=eval(input("y? "))

  x.append(c)

  y.append(d)

x.append(a)

y.append(b)



# plot routine

plt.cls()

plt.title("Polygon")

plt.window(-16,16,-10.5,10.5)

plt.axes("on")

plt.grid(1,1,"dot")

plt.color(75,0,130)

plt.pen("medium","solid")


for i in range(n):

  x0=x[i]

  y0=y[i]

  x1=x[i+1]

  y1=y[i+1]

  plt.line(x0,y0,x1,y1,"")


plt.show_plot()



TI-84 PLUS CE Python Script: INVFUNC8.py





Define f(x) in the def f(x) function routine. The math module is imported.



import ti_plotlib as plt

from math import *


# f(x) and f**-1(x)

# define f(x)

def f(x):

  return x**2+6


# plot routine

plt.cls()

plt.title("f(x) and its inverse")

plt.window(-16,16,-10.5,10.5)

plt.axes("on")

plt.grid(1,1,"dot")


plt.pen("medium","solid")


for i in range(320):

  x=-16+i*32/320

  y=f(x)

  plt.color(0,0,192)

  plt.plot(x,y,"o")

  plt.color(255,165,0)

  plt.plot(y,x,"o")



plt.show_plot()



Download the four scripts here: https://drive.google.com/file/d/1ELL6mEzMXrIJlOSSFocOZGJTza-kBZoU/view?usp=sharing



Eddie


All original content copyright, © 2011-2024. 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.


Saturday, April 22, 2023

Python: Drawing Regular Polygons (TI-84 CE Python, Numworks)

Python:  Drawing Regular Polygons (TI-84 CE Python, Numworks)



Introduction



The following scripts uses the Turtle module to draw regular polygons.   It turns out that every set of commands is slightly different in every calculator.  


For instance, there are no color names in the TI-84 Plus CE Python but they are present in Numworks.  


Also, the turtle module needs a t. (t-point) suffix of all turtle commands for the TI-84 Plus CE Python.  The Numworks turtle module does not need a suffix. 



TI-84 Plus CE Python:  dpolygon.py


from turtle import *

t=Turtle()


# initialization

s=60

n=int(input("# of sides? "))


# angle

a=180*(n-2)/n


# set turtle for TI-84

# color

k=-1

while k<0  or  k>4:

  print("Choose Color")

  print("0. blue")

  print("1. red")

  print("2. green")

  print("3. orange")

  print("4. indigo")

  k=int(input())

if k==0:

  t.pencolor(0,0,255)

if k==1:

  t.pencolor(255,0,0)

if k==2:

  t.pencolor(0,192,0)

if k==3:

  t.pencolor(255,165,0)

if k==4:

  t.pencolor(75,0,130)


t.clear()


# setup

t.penup()

t.left(180)

t.forward(60)

t.left(90)

t.forward(60)

t.left(90)


# draw the polygon

t.pendown()

for i in range(n):

  if n>6:

    t.forward(s/2)

  else:

    t.forward(s)

  t.left(180-a)

t.done()




Numworks Python:  dpythonnw.py


from math import *

from turtle import *


# initialization

s=60

n=int(input("# of sides? "))


# angle

a=180*(n-2)/n


# set color numworks

k=-1

l=['blue','red','green','orange','purple']

while k<0 or k>4:

  print("Choose Color")

  print("0. blue")

  print("1. red")

  print("2. green")

  print("3. orange")

  print("4. purple")

  k=int(input())

col=l[k]


# setup

penup()

left(180)

forward(60)

left(90)

forward(60)

left(90)


# draw the polygon

pendown()

color(col)

for i in range(n):

  if n>6:

    forward(s/2)

  else:

    forward(s)

  left(180-a)




Until next time,


Eddie 


All original content copyright, © 2011-2023.  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. 


Friday, July 19, 2019

TI Nspire CX II and TI Nspire CX II CAS: Drawing Demo

TI Nspire CX II  and TI Nspire CX II CAS:  Drawing Demo

Introduction

The new TI Nspire CX II  and TI Nspire CX II CAS has an expanded library of programming commands.  We are now able to draw and place text anywhere on the screen. 

When a drawing command is used, the Nspire shifts into graphics mode.  The graphics screen is treated as a final output screen.  Graphics mode only executes in one of two places:

*  Running the program in a Calculator page in a document
*  Running the program in the Calculator part of the Sketchpad

When a graphics screen has an initial (and maximum) height of 212 pixels and an initial (and maximum) width of 318 pixels.  The default background is white. 

Shifting a Program to Graphics Mode

The Nspire shifts into graphics mode automatically any time a drawing command is executed.  Followed by their basic syntax, the commands are:

Clear (blank to clear the entire screen),  (x, y, width, height)

DrawArc  x, y, width, height, startAngle, sweepAngle

DrawCircle x, y, radius

DrawLine x1, y1, x2, y2

DrawPoly (see plotdemo2 for further details)

DrawRect upper_x_pixel, upper_y_pixel, width, height

DrawText x, y, string/text/expression

FillArc x, y, width, height, startAngle, sweepAngle

FillCircle x, y, radius

FillPoly (see plotdemo2 for further details)

FillRect upper_x_pixel, upper_y_pixel, width, height

getPlatform() (returns dt for desktop, hh for handheld, ios for the iOS App)

PaintBuffer  (buffers the paint screen)

PlotXY x, y, shape  (13 shapes available: 1 for dot, 5 for cross, 6 for plus, 8, for medium dot)

SetColor  red, green, blue    (sets the Nspire's color pen)

SetPen thickness, style  (1 thin, 2 medium, 3 thick; 1 smooth, 2, dotted, 3 dashed)

SetWidnow xMin, xMax, yMin, yMax  (establishes drawing area - use this command to switch coordinates to Cartesian)

UseBuffer  (tells the Nspire to use an off screen to draw objects, then use
PaintBuffer to call them back up)

Once graphics mode is entered, the following commands cannot be used:

Request
RequestStr
Text

This means if you want the user to able to input during graphics mode, you will need to use Getkey.

When the Graphics Is Displayed

The graphics will be displayed until the user presses [ enter ]. 

Caution:   The drawing commands are only available for the CX II.  The original CX, nor any previous incarnations of the TI Nspire will not have these commands. 

TI NSpire CX II Program plotdemo1

This demo shows all the possible shapes that PlotXY has.  Colors are set randomly. 

Define plotdemo1()=
Prgm
:© graphics uses pixels: 318 * 212
:© Plotxy demo
:Local r,g,b,n,x,y
:For n,1,13
:© set colors
:r:=randInt(0,255)
:g:=randInt(0,255)
:b:=randInt(0,255)
:SetColor r,g,b
:© draw text and points
:DrawText 1+22*n,40,n
:PlotXY 5+22*n,50,n
:EndFor
:EndPrgm



TI NSpire CX II Program plotdemo2

This demo draws filled polygons.  There are two syntax sets for DrawPoly and FillPoly:

Draw/FillPoly  x1, y1, x2, y2, ... ,xn, yn

With this format the polygon stops at the coordinate (xn, yn).  The polygon is not automatically completed under this format.  To complete the polygon, make sure that the coordinate (x1, y1) is the last two coordinates listed.

Draw/FillPoly x_coordinate_list, y_coordinate_list

With this format, the polygon is automatically completed.

Define plotdemo2()=
Prgm
:© draw four triangles, filled
:Clear 
:© draw background box - gray
:© use fill, no draw
:SetColor 128,128,128
:FillRect 0,0,212,212
:© remember: x,y,width,height
:
:© now the triangles
:SetColor 128,0,0
:FillPoly 56,0,106,106,156,0
:FillPoly 56,212,106,106,156,212
:SetColor 127,255,255
:FillPoly 0,56,106,106,0,156
:FillPoly 212,56,106,106,212,156
:EndPrgm



TI Nspire CX II Program plotdemo3

This demo draws the function y = 1.5 cos (x^2).  The angle is set to radians mode.  The coordinates are converted to pixels prior to plotting them.

Define plotdemo3()=
Prgm
:© draw y=1.5cos(x^((2)))
:Clear 
:© radians
:setMode(2,1)
:© denim color
:SetColor 16,36,192
:© main
:Local x,xs,xp,y,ys,yp
:xs:=((4*Ï€)/(319))
:ys:=((4)/(213))
:For x,−2*Ï€,2*Ï€,xs
:  y:=1.5*cos(x^(2))
:  xp:=((x+2*Ï€)/(xs))
:  yp:=((−(y-2))/(ys))
:  DrawText 10,10,xp
:  PlotXY xp,yp,1
:EndFor
:EndPrgm

TI Nspire CX II Program plotdemo5

Like in plotdemo3, this program draws y = 1.5 cos (x^2).  Instead of converting coordinates to pixels, SetWindow is used and adjust the coordinates so that Cartesian coordinates can be used. 

Define plotdemo5()=
Prgm
:© draw y=1.5cos(x^((2)))
:Clear 
:© radians
:setMode(2,1)
:© denim color
:SetColor 16,36,192
:© main
:Local x,xs,y,ys
:© only x scaling is necesary
:xs:=((4*Ï€)/(319))
:SetWindow −2*Ï€,2*Ï€,−2,2
:© changes pixels to Cartesian
:For x,−2*Ï€,2*Ï€,xs
:  y:=1.5*cos(x^(2))
:  PlotXY x,y,1
:EndFor
:
:EndPrgm



This is a basic demonstration of drawing with the TI Nspire CX II.  On the next post, I show how to use getKey in graphics mode.

Happy drawing,

Eddie


All original content copyright, © 2011-2019.  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.

Monday, November 9, 2015

HP Prime Geometry App Tutorial Part 6: Polygons

HP Prime Geometry App Tutorial Part 6:  Polygons

Drawing a Regular Polygon

1.  Clear the Plot screen.  Press (Cmds), 4 for Polygon, 9 for Regular Polygon. 
2.  Select one corner and press [ Enter ].
3.  Select another corner point and press [ Enter ].
4.  Enter the number of sides of the regular polygon.



Drawing a General Polygon

1.  Press (Cmds), 4 for Polygon, 8 for Polygon.
2.  Start with a corner point and press [ Enter ].  Keep going choosing corner points and pressing [ Enter ]. 
3.  When you finished plotting the final corner point, press [ Enter ] one last time.  This connects the last corner point with the first corner point.   Hence, it is like go to the final point and press [ Enter ] twice.


Finding Areas of the Polygon

To recall, press (Cmds), 9 for Measure, 5 for Area.  Select a polygon (when selected they turned red) and press [ Enter ].




Next time, we will work with conic sections.   Have a great day,

Eddie

This blog is property of Edward Shore – 2015.



HP 15C: The I Squared Rule and HHC 2026

HP 15C: The I Squared Rule and HHC 2026 HHC 2026 Videos Before we start, the videos for HHC 2026 are up! Please take a look, ev...