PDA

View Full Version : Example: Section 5.2 (page 38/39), Plot Point, Diagonale + mirror 2D Functions



Lionheart008
26-02-2010, 16:25
page 38/39: it's nearly perfect. I have taken two tasks into one. changed colour of plot points to make them different. there are in my eyes four task (lines, points, mirror points, diagonale), all are solved for me. next part will follow later. my doggy wants to walk out now ;)


' Empty GUI script created on 02-26-2010 14:09:03 by (ThinAIR)

' Example 5.25 Plotting 2D Functions

' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming

' Converted by Frank Brübach
' Last modified: February 28, 2010


' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"


' Handle for our window
Local hWnd As DWord

' Create and show window
hWnd = TBGL_CreateWindowEx("Page 38: Plot Point + Mirror by Frank :)", 400, 400, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -1, -1, 1, 1 )

' Set background from default black to white
TBGL_BackColor(255,255,255)

' Resets status of all keys
TBGL_ResetKeyState()

Local x,y, x1,y1, x2,y2 As Single
' Main loop
While TBGL_IsWindow(hWnd)

TBGL_ClearFrame
TBGL_Color(0,0,0)
TBGL_PointSize 3
x = 0.5
y = 0.5

TBGL_BeginPoly(%GL_LINES)
TBGL_Vertex(-1.0,0.0)
TBGL_Vertex(1.0,0.0)
TBGL_Vertex(0.0,1.0)
TBGL_Vertex(0.0,-1.0)
TBGL_EndPoly

TBGL_BeginPoly(%GL_LINES)
TBGL_Vertex(-1.0,-1.0)
TBGL_Vertex(1.0,1.0)
TBGL_EndPoly


TBGL_Color(255,0,0)

'one
TBGL_PointSize 3
x = y*x
TBGL_BeginPoly(%GL_POINTS)
TBGL_Vertex(x,y)
TBGL_EndPoly
'two
TBGL_PointSize 3
y = x*y
TBGL_BeginPoly(%GL_POINTS)
TBGL_Vertex(x,y)
TBGL_EndPoly
'three
TBGL_Color(0,0,255)
TBGL_PointSize 3
'mirror x !
TBGL_BeginPoly(%GL_POINTS)
TBGL_Vertex(x*(+1),y*(-1))
TBGL_EndPoly
'four
TBGL_Color(0,0,255)
TBGL_PointSize 3
'mirror y !
TBGL_BeginPoly(%GL_POINTS)
TBGL_Vertex(x*(-1),y*(+1))
TBGL_EndPoly

'-------------------------------------------------
'-----------part one normal
'glColor3f(0.0, 0.0, 0.0)
'glBegin(GL_LINES)
'glVertex2f(-1.0, 0.0)
'glVertex2f(1.0,0.0)
'glVertex2f(0.0, 1.0)
'glVertex2f(0.0, -1.0)
'glEnd()
'# Store an ordered pair In variables
'x = 0.5
'y = 0.5
'# Plot points In bright red
'glColor3f(1.0, 0.0, 0.0)
'# Increase the point Size
'glPointSize(3.0)
'glBegin(GL_POINTS)
'# Plot the point
'glVertex2f(x, y)
'-----------part two mirror!
'# Plot the mirror Image Or reflection of the point
'# In the x axis.
'glVertex2f(-1.0,-1.0)
'glVertex2f(1.0, 1.0)
'
'---------------------------------------------------------

TBGL_DrawFrame

' ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

'-I am not quite sure if this is 100 per cent right conversion for this solution ;)

edit: fixed version I add with correct window scaling and sizing

best regards, frank lionhead

sblank
26-02-2010, 16:59
Nicely done Frank!

This was an exercise in symmetry for my students (and me). I was mainly trying to demonstrate symmetry across both the x and y axes and across the y = x diagonal (leading to inverse functions later, perhaps). I was wanting students to understand the role of switching signs from + to - and - to + for the ordered pairs (x, y) AND to see what happens when we switch x and y, plotting (y, x). Sometimes we get interesting visual and mathematical effects... sometimes almost like a kaleidoscope.

I have never decided whether I was trying to teach a little programming using mathematics or to teach a little mathematics using programming. No matter... the students seemed to enjoy the experience for the most part and so did I.

Thanks for your interest and help! I am the complete beginner here and I'm learning by everyone sharing their programs.

Stan

Michael Hartlef
26-02-2010, 17:13
Hi Stan,

As i see that Franck add his personal style to the code i would like to ask if you would like a pure conversion to thinbasic or something modded?

Michael

sblank
26-02-2010, 19:20
Hi Michael,

Thanks for asking... the quick answer is that it really doesn't matter to me. I encourage my students to be creative and never intended the examples to be anything more than a starting point. Each exercise and example had a purpose when I wrote them, but it is always interesting to see what new ideas people come up with. The pure conversion would be easy to code based on what Frank has done, so no worries there at all.

If Frank were in my class, I probably would want to see the original graphic with all symmetries displayed, but after that, students are free to create new versions and go wherever their imagination and code takes them. Also, I make no claims for elegance in coding! I tend to think in a very linear fashion and I'm not very creative, so I would encourage suggestions, improvements, modifications, etc.

This is all very flattering to me, really, that anyone (not in my class!) would take the time to look through the text and start converting some of the examples to ThinBasic. I was more than willing to share the text, but really didn't expect this kind of response. There are so many great programmers here and I feel so inadequate... I have a lot of learning to do!

So, Frank can just code away however he sees fit... :D

I do think that as a service to others here, though, if the conversion does deviate from the original Python version it would be nice to communicate the differences? That's just my suggestion/opinion.

Cheers... and thanks!

Stan



Hi Stan,

As i see that Franck add his personal style to the code i would like to ask if you would like a pure conversion to thinbasic or something modded?

Michael

kryton9
27-02-2010, 01:14
Glad you are joining the fun Frank. It is nice to have others getting into it too.

sblank
27-02-2010, 05:28
Yes... certainly! Hopefully I didn't sound critical in my previous post, because that was NOT my intention.

Code to your heart's content, Frank... welcome!

Cheers,

Stan



Glad you are joining the fun Frank. It is nice to have others getting into it too.