Notifications
Clear all

[Closed] Function editor

I’m wondering if anyone has any bright ideas on how to implement a function editor as a dailog control, where the user sees essentially quadrant 1, and a polynomial equation is graphed in it, where the user can click to add points in the equation. Is this impossible to do?

5 Replies
1 Reply
(@bobo)
Joined: 2 years ago

Posts: 0

Why would it be impossible?
I would suggest starting with the basics, create a bitmap control to draw into placed in a dialog so you can use all mouse handlers, add the entry field, try to plot some simple functions, then expand from there and tell us what problems you have enountered.

Finally got a chance to try this out, your idea worked great Bobo!

In case anyone else would like to take advantage of this…


rollout graphEditor "Function Editor" width:220 height:339
(
 bitmap graph "Bitmap" pos:[27,31] width:181 height:65
 editText tymin "" pos:[26,94] width:28 height:19 text:"0"
 editText tymax "" pos:[179,95] width:28 height:19 text:"1"
 editText txmin "" pos:[-1,76] width:28 height:19 text:"0"
 editText txmax "" pos:[-1,32] width:28 height:19 text:"10"
 listbox lbpoly "Polynomial Points" pos:[0, 120]
 
 local xmin = 0
 local xmax = 10
 local ymin = 0
 local ymax = 1
 local graphImage = bitmap 181 65 color:white
 local polypoints = #()
 local isDrawing = false
 
 on tymin changed val do
  ymin = (val as Integer)
 on tymax changed val do
  ymax = (val as Integer)
 on txmin changed val do
  xmin = (val as Integer)
 on txmax changed val do
  xmax = (val as Integer)
 
 fn drawLine thebmp start end=
 (
  --y=mx+b
  m = (end.y - start.y)/(end.x-start.x)
  b = end.y - m*end.x
  
  for bmpx = start.x to end.x do
  (
   bmpy = (m*bmpx + b)
   setPixels thebmp [bmpx,bmpy] #(black)
  )
 )
 
 fn bmpToFuncPoint point=
 (
  x = point.x/graph.width * (xmax-xmin) + xmin
  y = (graph.height - point.y)/graph.height * (ymax-ymin) + ymin
  
  return [x,y]
 )
 
 fn record pos=
 (
  setPixels graphImage pos #(black)
  graph.bitmap = graphImage
 )
 
 fn estYFromGraph bmpx=
 (
  x = bmpx
  found = false
  sign = -1
  count = 0
  
  graphImage = graph.bitmap
  
  while (found != true and count < 20) do
  (  
   y = 0
   while y < graph.height and found != true do
   (
	val = (getPixels graphImage [x,y] 1)[1]
	
	if val==black then 
	 found = true
	else 
	 y += 1
   )
   
   count += 1
   sign *= -1
   x += sign*count
  )
  
  if (found != true) then
   y = graph.height
  
  return y
 )
 
 on graphEditor lbuttondown pos do
 (
  isDrawing = true
  polypoints=#()
  record (pos-graph.pos)
 )
 
 on graphEditor lbuttonup pos do 
 (
  isDrawing = false
  newbmp = bitmap 181 65 color:white
  step = (graph.width/10)
  lastpt = [0,(estYFromGraph 0)]
  append polypoints (bmpToFuncPoint lastpt)
  
  for bmpx = step to graph.width by step do
  (
   bmpy = estYFromGraph bmpx
   drawLine newbmp lastpt [bmpx,bmpy]
   lastpt = [bmpx,bmpy]
   append polypoints (bmpToFuncPoint lastpt)
  )
  
  lbpoly.items=#()
  for p in polypoints do
  (
   lbpoly.items = append lbpoly.items (p as string)
  )
  
  graphImage = bitmap 181 65 color:white
  graph.bitmap = newbmp
 )
 
 on graphEditor mousemove pos do
  if isDrawing do record (pos-graph.pos)
 
)
createDialog graphEditor

Thanks for the script

Are you trying to model mathematical functions ?

This could be useful: GeoGebra.
http://www.geogebra.at/index.php?=&lang=fr&lang=en

If there is a variable that the user wishes to control, I could just give them a spinner for it…but that means the variable needs to remain constant.

I would rather give the user the ability to dynamically control a variable based on another variable (such as time, or something else)…by using an equation. In XFrog you can do this by entering equations, or by adding points to polynomial equations…in Maya as I recall they have something like this…but basically the point is to be able to quickly and easily describe a function without having to pick from a list of pre-defined functions. Here, all you do is set the axis and then quickly sketch, and it uses your sketch to create a complete step-function for it.

Currently I use my graphing calculator or mathematica or sometimes matlab, but this soft could come in handy too, thanks.