Periodic table

Moderators: LCMark, LCfraser

Post Reply
[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1520
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Periodic table

Post by [-hh] » Thu Oct 20, 2016 5:18 am

Dave B. wrote:[Cited from the use-list)
I'm trying to build a chemical periodic table widget. I can do this up to
the point of having it look like a periodic table (nice arrangement,
symbols in correct places). My plan depends on being able to store and
retrieve a rectangle for each element. I CAN compute an appropriate
rectangle for each chemical element that places it properly in a periodic
table. My plan depends on being able to store and retrieve a rectangle for
each element.

I want to create an array that has the 'usual' chemical information. I want
to include in that array the computed rectangle for each element such that
it can redraw the widget when a user reshapes the widget and can detect a
clickloc in the rectangle of the element to retrieve data

I've tried to store the rectangle in the array --and retrieve it -- without
success. I think the issue is that when it goes into the array it does so
as a string. I've tried to store 4 Numbers in the array for each chemical
element -- corresponding to the x,y of the top-left and bottom right of the
rectangle and then recreate the rectangle from those numbers. No success.
I've tried to store those as Integers and recreate them with commas into a
variable typed as an array. No success.
If I understand correctly what you wish to do, here is a LCB-snippet, that may work in that direction (not tested, right out of my head).

Code: Select all

private variable mNumOfElements as Number
private variable mCurrentElement as Number
private variable mCurrentRect as Rectangle
private variable mElements as Array

public handler OnCreate()
	put 118 into mNumOfElements
	put getPeriodicTable() into mElements
	-- ...
end handler

private handler getPeriodicTable() returns Array
	variable tTable as Array
	-- Element 1
	put the empty array into tTable["1"]
	put 1 into tTable["1"]["Group"]
	put 1 into tTable["1"]["Period"]
	put "H" into tTable["1"]["Element"]
	put "Hydrogen" into tTable["1"]["ElementName"]
	put rectangle [20,20,80,80] into tTable["1"]["Rect"]
	-- Element 2
	put the empty array into tTable["2"]
	put 18 into tTable["2"]["Group"]
	put 1 into tTable["2"]["Period"]
	put "He" into tTable["2"]["Element"]
	put "Helium" into tTable["2"]["ElementName"]
	put rectangle [420,20,480,80] into tTable["2"]["Rect"]
	-- .... 
	return tTable
end handler

public handler OnMouseDown()
	variable tRect as Rect
	variable tPoint as Point
	variable tCount as Number
	put the click position into tPoint
	repeat with tCount from 1 up to mNumOfElements
		put mElements[(tCount formatted as string)]["rect"] into tRect
		if tPoint is within tRect then
			exit repeat
		end if
	end repeat
	put tCount into mCurrentElement
	put tRect into mCurrentRect
	-- ...
end handler
shiftLock happens

peter-b
Posts: 182
Joined: Thu Nov 20, 2014 2:14 pm
Location: LiveCode Ltd.

Re: Periodic table

Post by peter-b » Thu Oct 20, 2016 10:52 am

Don't forget that we have array literal syntax in LCB now, so you could write:

Code: Select all


widget foo.bar.periodic_table

constant kElementData is { \
  "1": { "group": 1, "period": 1, "element": "H", "elementName", "Hydrogen" }, \
  "2": { "group": 18, "period", 2, "element": "He", "elementName", "Helium" }, \
  /* etc... */ \
}

end widget
Putting the data into a constant will currently be more efficient because the array will be constructed only once (and very quickly) each time the widget is loaded (no matter how many widgets you create and destroy).
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com

dbrooksne
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4
Joined: Thu May 04, 2006 7:55 pm
Location: University of Nebraska
Contact:

Re: Periodic table

Post by dbrooksne » Sat Nov 05, 2016 11:03 pm

This handler hangs on the definition of the rectangle (put [(tX-1)*10,(tY-1)*10,tX*10,tY*10] into tRect1) (Any help appreciated):

private handler DrawGrid()

variable tPath as Path
variable tRect1 as Rectangle
variable tInput
put [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, \
3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 6, 7, 8, 9, 10,\
11, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18] into tInput

variable tX
put 1 into tX
variable tY
put 1 into tY
variable tValue
set the paint of this canvas to solid paint with color [0,0,0]
repeat for each element tValue in tInput
if tValue is not 0 then
put [(tX-1)*10,(tY-1)*10,tX*10,tY*10] into tRect1
put rectangle path of tRect1 into tPath
stroke tPath on this canvas
end if

add 1 to tX
if tX > 19 then
put 1 into tX
add 1 to tY
end if
end repeat
end handler
David W. Brooks

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1520
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Periodic table

Post by [-hh] » Sun Nov 06, 2016 12:40 am

Hi Dave.

tRect1 is a rectangle, not a list. So we have to write:

Code: Select all

put rectangle [(tX-1)*10,(tY-1)*10,tX*10,tY*10] into tRect1
p.s. You could also use LCB snippet #44: Create a matrix of rectangles of constant size.

Hope you show us the finished work.

Hermann
shiftLock happens

Post Reply

Return to “LiveCode Builder”