Ton Le
Ton Le

Reputation: 25

Python Programming: plotting points with mathematical expressions

So i need to be able to plot a point on a graph made by my already coded cartesian coordinate system. The geometry is like: (0,0) is at the top left of the window and as it goes right, the x increases and as it goes down, the y increases so the bottom right corner would be (800, 600).

My cartesian (0,0) is actually on the point (400, 300) and that's where I want my graphs to be aligned to.

My code for getting input, converting it to an expression and graphing these points using small rectangular dots is

expression = input("Enter a mathematical 
for x in range(0, 800):
    y = eval(expression)
    rect(x, y, 2, 2)

My problem is: the code needs to be able to read and graph all normal mathematical expressions properly such as x, x^2, x^3, etc., but on my drawn cartesian plane, the values are actually all positives due to the window's weird quadrant system created by the graphics library.

I'm not getting the correct plotting when my program starts to plot out and map all these coordinates.

Could someone shed some light on what im supposed to do in terms of actually converting these graphics coordinates to match my cartesian plane coordinates?

NOTE every 30 graphics units = 1 tick unit of my cartesian plane.

Upvotes: 0

Views: 277

Answers (1)

lingxiao
lingxiao

Reputation: 1224

if your problem is what i think it is, try the following code.

expression = input("Enter a mathematical 
for x in range(0, 800):
    x_val = x-400
    y_val = eval(expression(x_val))
    y = -y_val+300
    rect(x, y, 2, 2)

Upvotes: 1

Related Questions