## calculating the slope

Mar 5, 2009 at 6:14am

# calculating the slope

Hey all here’s a (hopefully quick) max math question for ya.
I’m trying to continuously calculate the slope between a set of changing x-y coordinates. How do I set up this equation without getting a stack overflow?
(y_new – y_old)/(x_new – x_old)
Thanks so much to anyone with time to help me out, let me know if you need the patch.
-Diane
PS if anyone still cares I’m just finishing off a couple of those LFO abstractions I was asking for help on before – I’ll post them as soon as they’re done!

#42675
Mar 5, 2009 at 7:19am

Here are two ways of doing what you want. One uses [bucket] to remember the previous input and the other waits for 4 items to be received before doing the math. I hope it helps.

lh

– Pasted Max Patch, click to expand. –
#152778
Mar 5, 2009 at 4:50pm

Hey thanks again lh! What you gave me is just what I’m looking for but I’m still having trouble setting it up. I’m trying to get it to continuously calculate the slope between two points as I send data to jit.lcd. Works fine if I’m just sending message box values one at a time, but when I send it a continuous stream of coordinates both the slope calculators seem to just fluctuate between 0 1 or -0. Does this have something to do with dividing by zero? Or did I connect things wrong? Here is what I was trying to patch together. Thank you so much!

{
“patcher” : {
“fileversion” : 1,
“rect” : [ 25.0, 69.0, 640.0, 506.0 ],
“bglocked” : 0,
“defrect” : [ 25.0, 69.0, 640.0, 506.0 ],
“openrect” : [ 0.0, 0.0, 0.0, 0.0 ],
“openinpresentation” : 0,
“default_fontsize” : 12.0,
“default_fontface” : 0,
“default_fontname” : “Arial”,
“gridonopen” : 0,
“gridsize” : [ 15.0, 15.0 ],
“gridsnaponopen” : 0,
“toolbarvisible” : 1,
“boxanimatetime” : 200,
“imprint” : 0,
“boxes” : [ {
"box" : {
"maxclass" : "flonum",
"fontsize" : 12.0,
"numinlets" : 1,
"numoutlets" : 2,
"patching_rect" : [ 365.0, 176.0, 50.0, 20.0 ],
“id” : “obj-8″,
“outlettype” : [ "float", "bang" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “flonum”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 2,
“patching_rect” : [ 315.0, 176.0, 50.0, 20.0 ],
“id” : “obj-6″,
“outlettype” : [ "float", "bang" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “pictslider”,
“numinlets” : 2,
“numoutlets” : 2,
“patching_rect” : [ 315.0, 60.0, 100.0, 100.0 ],
“id” : “obj-9″,
“outlettype” : [ "int", "int" ]
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “pack f f”,
“fontsize” : 12.0,
“numinlets” : 2,
“numoutlets” : 1,
“patching_rect” : [ 315.0, 204.0, 49.0, 20.0 ],
“id” : “obj-15″,
“outlettype” : [ "" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “message”,
“text” : “\$1 \$2″,
“fontsize” : 12.0,
“numinlets” : 2,
“numoutlets” : 1,
“patching_rect” : [ 315.0, 230.0, 41.0, 18.0 ],
“id” : “obj-14″,
“outlettype” : [ "" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “zl join”,
“fontsize” : 12.0,
“numinlets” : 2,
“numoutlets” : 2,
“patching_rect” : [ 315.0, 254.0, 41.0, 20.0 ],
“id” : “obj-69″,
“outlettype” : [ "", "" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “zl slice 4″,
“fontsize” : 12.0,
“numinlets” : 2,
“numoutlets” : 2,
“patching_rect” : [ 315.0, 276.0, 57.0, 20.0 ],
“id” : “obj-54″,
“outlettype” : [ "", "" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “flonum”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 2,
“patching_rect” : [ 315.0, 345.0, 50.0, 20.0 ],
“id” : “obj-48″,
“outlettype” : [ "float", "bang" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “expr (\$f4-\$f2)/(\$f3-\$f1)”,
“fontsize” : 12.0,
“numinlets” : 4,
“numoutlets” : 1,
“patching_rect” : [ 315.0, 323.0, 131.0, 20.0 ],
“id” : “obj-46″,
“outlettype” : [ "" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “unpack 0. 0. 0. 0.”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 4,
“patching_rect” : [ 315.0, 301.0, 130.0, 20.0 ],
“id” : “obj-45″,
“outlettype” : [ "float", "float", "float", "float" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “flonum”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 2,
“patching_rect” : [ 180.0, 345.0, 50.0, 20.0 ],
“id” : “obj-29″,
“outlettype” : [ "float", "bang" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “!/ 1.”,
“fontsize” : 12.0,
“numinlets” : 2,
“numoutlets” : 1,
“patching_rect” : [ 180.0, 323.0, 80.5, 20.0 ],
“id” : “obj-26″,
“outlettype” : [ "float" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “- 0.”,
“fontsize” : 12.0,
“numinlets” : 2,
“numoutlets” : 1,
“patching_rect” : [ 242.0, 301.0, 32.5, 20.0 ],
“id” : “obj-23″,
“outlettype” : [ "float" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “t f f”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 2,
“patching_rect” : [ 242.0, 257.0, 32.5, 20.0 ],
“id” : “obj-24″,
“outlettype” : [ "float", "float" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “bucket”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 1,
“patching_rect” : [ 256.0, 279.0, 46.0, 20.0 ],
“id” : “obj-25″,
“outlettype” : [ "" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “- 0.”,
“fontsize” : 12.0,
“numinlets” : 2,
“numoutlets” : 1,
“patching_rect” : [ 180.0, 301.0, 32.5, 20.0 ],
“id” : “obj-3″,
“outlettype” : [ "float" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “t f f”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 2,
“patching_rect” : [ 180.0, 257.0, 32.5, 20.0 ],
“id” : “obj-2″,
“outlettype” : [ "float", "float" ],
“fontname” : “Arial”
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “bucket”,
“fontsize” : 12.0,
“numinlets” : 1,
“numoutlets” : 1,
“patching_rect” : [ 194.0, 279.0, 46.0, 20.0 ],
“id” : “obj-1″,
“outlettype” : [ "" ],
“fontname” : “Arial”
}

}
],
“lines” : [ {
"patchline" : {
"source" : [ "obj-8", 0 ],
“destination” : [ "obj-24", 0 ],
“hidden” : 0,
“midpoints” : [ 374.5, 198.0, 251.5, 198.0 ]
}

}
, {
“patchline” : {
“source” : [ "obj-6", 0 ],
“destination” : [ "obj-2", 0 ],
“hidden” : 0,
“midpoints” : [ 324.5, 198.0, 189.5, 198.0 ]
}

}
, {
“patchline” : {
“source” : [ "obj-9", 1 ],
“destination” : [ "obj-8", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-9", 0 ],
“destination” : [ "obj-6", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-8", 0 ],
“destination” : [ "obj-15", 1 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-6", 0 ],
“destination” : [ "obj-15", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-1", 0 ],
“destination” : [ "obj-3", 1 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-2", 0 ],
“destination” : [ "obj-3", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-2", 1 ],
“destination” : [ "obj-1", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-24", 1 ],
“destination” : [ "obj-25", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-24", 0 ],
“destination” : [ "obj-23", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-25", 0 ],
“destination” : [ "obj-23", 1 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-3", 0 ],
“destination” : [ "obj-26", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-23", 0 ],
“destination” : [ "obj-26", 1 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-26", 0 ],
“destination” : [ "obj-29", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-54", 0 ],
“destination” : [ "obj-45", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-45", 0 ],
“destination” : [ "obj-46", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-45", 1 ],
“destination” : [ "obj-46", 1 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-45", 2 ],
“destination” : [ "obj-46", 2 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-45", 3 ],
“destination” : [ "obj-46", 3 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-46", 0 ],
“destination” : [ "obj-48", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-69", 0 ],
“destination” : [ "obj-54", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-54", 0 ],
“destination” : [ "obj-69", 1 ],
“hidden” : 0,
“midpoints” : [ 324.5, 298.0, 383.0, 298.0, 383.0, 251.0, 346.5, 251.0 ]
}

}
, {
“patchline” : {
“source” : [ "obj-15", 0 ],
“destination” : [ "obj-14", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
, {
“patchline” : {
“source” : [ "obj-14", 0 ],
“destination” : [ "obj-69", 0 ],
“hidden” : 0,
“midpoints” : [ ]
}

}
]
}

}

#152779
Mar 5, 2009 at 10:13pm

its because the pictslider sends out a new set of values every time one of its values is changed by 1. So you’ll only ever get a slope of +1, 0, -1, or + or – infinity. What you need to do is store the output of the slider with zl reg or two float objects and then bang it out at regular intervals with metro (say a few times a sec ). This will give you meaningful slope values, as it is then relative to time, rather than to the last time a value was output.

ok, heres a patch:

– Pasted Max Patch, click to expand. –
#152780
Mar 5, 2009 at 11:48pm

THANK YOU! That works perfectly! to make it work with jit.lcd all I had to do was invert the y axis variable so that I wasn’t getting large y values when the curser was at the bottom of the screen. Sweet… thank you.
-Diane

#152781

You must be logged in to reply to this topic.