This repository was archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 120
api calculate position
Jesse Freeman edited this page Nov 11, 2021
·
1 revision
Converts an index value into a point with an X and Y value to help when working with 1D arrays that represent 2D data.
CalculatePosition ( index, width )| Name | Value | Description |
|---|---|---|
| index | int | The position of the 1D array. |
| width | int | The width of the data if it was a 2D array. |
| Value | Description |
|---|---|
| int | Returns a vector representing the X and Y position of an index in a 1D array. |
In this example, we will treat a 1D as a 2D array and convert an index into a X, Y position. Running this code will output the following:
![]()
-- A 1D array of example values
local exampleGrid = {
"A", "B", "C",
"D", "E", "F",
"G", "H", "I",
}
function Init()
-- Example Title
DrawText("CalculatePosition()", 8, 8, DrawMode.TilemapCache, "large", 15)
DrawText("Lua Example", 8, 16, DrawMode.TilemapCache, "medium", 15, -4)
local index = 4
-- Calculate the center index based on a grid with 3 columns
local position = CalculatePosition(index, 3)
-- Draw the index and value to the display
DrawText("Position " .. position.x .. ",".. position.y .. " at Index " .. index .. " is " .. exampleGrid[index], 1, 4, DrawMode.Tile, "large", 15)
end
function Draw()
-- Redraw the display
RedrawDisplay()
endnamespace PixelVision8.Player
{
class CalculatePositionExample : GameChip
{
// A 1D array of example values
private string[] exampleGrid =
{
"A", "B", "C",
"D", "E", "F",
"G", "H", "I",
};
public override void Init()
{
// Example Title
DrawText("CalculatePosition()", 8, 8, DrawMode.TilemapCache, "large", 15);
DrawText("C Sharp Example", 8, 16, DrawMode.TilemapCache, "medium", 15, -4);
var index = 4;
// Calculate the center index based on a grid with 3 columns
var position = CalculatePosition(index, 3);
// Draw the index and value to the display
DrawText("Position " + position.X + "," + position.Y + " at Index " + index + " is " + exampleGrid[index], 1, 4, DrawMode.Tile, "large", 15);
}
public override void Draw()
{
// Redraw the display
RedrawDisplay();
}
}
}- Game Project
- Lua Games (coming soon)
- C# Sharp Games
- C# Sharp Vs Lua
- API Cheat Sheet
- Enums
- Visual Studio Code
- Atom
- Tiled (Coming Soon)
- Aseprite (Coming Soon)
- AddScript
- BackgroundColor
- Button
- CalculateDistance
- CalculateIndex
- CalculatePosition
- CharacterToPixelData
- Clamp
- Clear
- Color
- ColorsPerSprite
- Display
- DrawMetaSprite
- DrawPixels
- DrawRect
- DrawSprite
- DrawText
- DrawTilemap
- Flag
- InputString
- IsChannelPlaying
- Key
- LoadTilemap
- MaxSpriteCount
- MouseButton
- MousePosition
- NewCanvas
- NewPoint
- NewRect
- PaletteOffset
- PauseSong
- PlaySong
- PlaySound
- ReadAllMetadata
- ReadMetadata
- ReadSaveData
- RedrawDisplay
- Repeat
- ReplaceColor
- RewindSong
- ScrollPosition
- SongData
- Sound
- SplitLines
- SpriteSize
- Sprite
- StopSong
- StopSound
- Tile
- TilemapSize
- TotalColors
- TotalSprites
- UpdateTiles
- WordWrap
- WriteSaveData