Opinion regarding included code

  • Thread starter woven
  • Start date
  • #1
1
0
Seeking opinion regarding included code.

//Processing - Processing.org
Code:
int _size = 256;
int _mx, _my;
Ordinate _x, _y, _one;

void setup()
{
  size(this._size, this._size, P2D);
  _x = new Ordinate(this._size);
  _y = new Ordinate(this._size);
  _one = Ordinate.One(this._size);
}

void draw()
{
  Ordinate o = Ordinate.Add(_x, _y);  
  for(int a = 0, x = 0, y = 0; a < o._magnitude.length; a += 3)
  {
    int col = 255 << 24 | o._magnitude[a] << 16 | o._magnitude[a + 1] << 8 | o._magnitude[a + 2];
    
    stroke(col);
    point(x, y);
    x = x + 1 == _size ? 0 : x + 1;
    y = x == 0 ? y + 1 : y;
  }
}

void keyPressed()
{
  if(key == 'w')
  {
    _x = Ordinate.Add(_x, _one);
  }
  else if(key == 's')
  {
     _x = Ordinate.Sub(_x, _one);
  }
  else if(key == 'a')
  {
     _y = Ordinate.Add(_y, _one);
  }
  else if(key == 'd')
  {
     _y = Ordinate.Sub(_y, _one);
  }
}

void mousePressed()
{
  _mx = mouseX;
  _my = mouseY;
}

void mouseReleased()
{
  int dx = mouseX;
  int dy = mouseY;
  int dragLength = (_mx - dx) * (_mx - dx) + (_my - dy) * (_my - dy);  
}

static class Ordinate
{
  public int _size;
  public byte[] _magnitude;
  
  private Ordinate()
  {
    this._size = 0;
    this._magnitude = null;
  }
  
  public Ordinate(int Size)
  {
    this._size = Size;
    this._magnitude = new byte[this._size * this._size * 3];
  }
  
  public static Ordinate One(int Size) { 
    Ordinate o = new Ordinate(Size); 
    o._magnitude[o._magnitude.length - 1] = 1; 
    return o;
  }

  public static Ordinate Add(Ordinate Ord1, Ordinate Ord2)
  {
    Ordinate o = new Ordinate(Ord1._size);
    for (int a = Ord1._magnitude.length - 1, carry = 0; a > -1; a--)
    {
      int v = Ord1._magnitude[a] + Ord2._magnitude[a] + carry;
      carry = v / 256;
      o._magnitude[a] = (byte)(v - carry * 256);
    }
    return o;
  }

  public static Ordinate Sub(Ordinate Ord1, Ordinate Ord2)
  {
    Ordinate o = new Ordinate(Ord1._size);
    for (int a = Ord1._magnitude.length - 1, borrow = 0; a > -1; a--)
    {
      if (Ord1._magnitude[a] >= (Ord2._magnitude[a] + borrow))
      {
        o._magnitude[a] = (byte)(Ord1._magnitude[a] - Ord2._magnitude[a] - borrow);
        borrow = 0;
      }
      else
      {
        o._magnitude[a] = (byte)((Ord1._magnitude[a] + 256) - Ord2._magnitude[a] - borrow);
        borrow = 1;
      }
    }
    return o;
  }
}
//c# wpf
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ordinate
{
    public partial class MainWindow : Window
    {
        Ord _x, _y;

        internal class Ord
        {
            private int _size;
            private byte[] _magnitude;
            public byte[] Magnitude { get { return _magnitude; } set { _magnitude = value; } }

            public static Ord One(int Size) { Ord o = new Ord(Size); o._magnitude[o._magnitude.Length - 1] = 1; return o; }

            public Ord(int Size)
            {
                this._size = Size;
                this._magnitude = new byte[this._size * this._size * 3];
            }

            public static Ord operator +(Ord Ord1, Ord Ord2)
            {
                if (Ord1._magnitude.Length != Ord2._magnitude.Length)
                    throw new ArgumentException("Operands must have equal dimensions");

                Ord o = new Ord(Ord1._size);

                for (int a = Ord1._magnitude.Length - 1, carry = 0; a > -1; a--)
                {
                    int v = Ord1._magnitude[a] + Ord2._magnitude[a] + carry;
                    carry = v / 256;
                    o._magnitude[a] = (byte)(v - carry * 256);
                }

                return o;
            }

            public static Ord operator -(Ord Ord1, Ord Ord2)
            {
                if (Ord1._magnitude.Length != Ord2._magnitude.Length)
                    throw new ArgumentException("Operands must have equal dimensions");

                Ord o = new Ord(Ord1._size);

                for (int a = Ord1._magnitude.Length - 1, borrow = 0; a > -1; a--)
                {
                    if (Ord1._magnitude[a] >= (Ord2._magnitude[a] + borrow))
                    {
                        o._magnitude[a] = (byte)(Ord1._magnitude[a] - Ord2._magnitude[a] - borrow);
                        borrow = 0;
                    }
                    else
                    {
                        o._magnitude[a] = (byte)((Ord1._magnitude[a] + 256) - Ord2._magnitude[a] - borrow);
                        borrow = 1;
                    }
                }

                return o;
            }

            internal static BitmapSource Render(Ord Ord1, Ord Ord2)
            {
                if (Ord1._magnitude.Length != Ord2._magnitude.Length)
                    throw new ArgumentException("Operands must have equal dimensions");

                WriteableBitmap buffer = new WriteableBitmap(Ord1._size, Ord1._size, 96, 96, PixelFormats.Rgb24, null);
                buffer.Lock();
                buffer.WritePixels(new Int32Rect(0, 0, Ord1._size, Ord1._size), (Ord1 - Ord2)._magnitude, Ord1._size * 3, 0);
                buffer.Unlock();
                return buffer;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }        

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _x = new Ord(256);
            _y = new Ord(256);
            renderBuffer.BeginInit();
            renderBuffer.Source = Ord.Render(_x, _y);
            renderBuffer.EndInit();
        }
    }
}
It seems fairly evident that as we created the system of symbolism we should be able to manipulate it at will and yet it seems a subject little spoken of. This code is simply using addition and subtraction within the base 256, a very basic implementation yet able to convey so much.

EDIT: I leave it to you to rework the code to suit whatever form of interaction you see fit, i have implemented a simple keypress system within the processing code, the extension of which should be easy for most. One might incorporate a system of gestures or mouse interaction to enable speedy transitions.
 
Last edited:

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
16,428
6,716


what is your question?
 

Related Threads on Opinion regarding included code

  • Last Post
Replies
7
Views
2K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
9
Views
4K
Replies
24
Views
12K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
3
Views
1K
Replies
33
Views
5K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
3
Views
2K
Top