get() whats the diff? - oop

Whats the difference between these two?
PVector a = new PVector(10,12);
PVector b = a;
and
PVector a = new PVector(10,12);
PVector b = a.get();
I realise get() returns a copy of the pvector, but whats the difference from the other one?
Thanks

In your first example, b and a reference the same instance of PVector. Therefore, if you change a.x or a.y, that change will be reflected in b as well.
The second copies the PVector. Changes to one won't be reflected in the other as they're separate instances of the object.

The answer is above, just to illustrate...
PVector a = new PVector(10,12);
PVector b = a.get();
PVector c = new PVector(10,12);
PVector d = c;
b.x = 5;
b.y = 7;
d.x = 3;
d.y = 6;
println("a = " + a + "\n");
println("c = " + c + "\n");

Related

Game Maker Studio 2 Array taking wrong values

Hey guys I'm new to Game Maker Studio and new to the language. I'm making a game and have been working on the dialogue system.
This chunk of code was designed for characters respond to a set of choices, the dialogue starts by printing out the first element of the line_array, which it does, then give the player the choice of two responses from the response_array, which it insteads prints out the second element of the line_array and I don't understand why.
Does an argument only hold one element of an array? I'm initializing two arrays in an object oCivilian2 and pushing them through code DialogueCode which is linked to another object oRespond that supposed to allow me to sift through dialogue in game. Anything helps thanks
It's initialized here in create of oCivilian2
line_array = [3];
line_array[0] = "Ethan it's good to see you! \n I thought after the incident well.... \n well I thought we had lost you";
line_array[1] = "I've said too much";
line_array[2] = "You hit your head trying to saver her\n It was horrible";
response_array = [2];
response_array[0] = "What happened?";
response_array[1] = "I don't recall alot. How bad was it?";
counter = 0;
x1 = RESOLUTION_W / 2;
y1 = RESOLUTION_H -70;
x2 = RESOLUTION_W/2;
y2 = RESOLUTION_H;
_print = "";
responseSelected = 0;
Then the step which links it to DialogueCode when spacebar is pressed
keyActivate = keyboard_check_pressed(vk_space);
if (keyActivate)
{
var inst = collision_rectangle(oPlayer.x+3,oPlayer.y+3,oPlayer.x-3,oPlayer.y-3, oCivilian2, false, false);
if (inst != noone)
{
ScriptExecuteArray(DialogueCode, line_array);
ScriptExecuteArray(DialogueCode, response_array);
}
}
Then through to step in the object oRespond
lerpProgress += (1 - lerpProgress) / 50;
textProgress += global.textSpeed;
x1 = lerp(x1, x1Target,lerpProgress);
x2 = lerp(x2, x2Target,lerpProgress);
keyUp = (keyboard_check_pressed(vk_up)) || (keyboard_check_pressed(ord("W")))
keyDown = keyboard_check_pressed(vk_down) || keyboard_check_pressed(ord("S"));
responseSelected += (keyDown - keyUp);
var _max = 2;
var _min = 0;
if (responseSelected > _max) responseSelected = _min;
if (responseSelected < _min) responseSelected = _max;
for (var i = 0; i < 2; i++)
{
var _marker = string_pos(",", response);
if (string_pos(",",response))
{
responseScript[i] = string_copy(response,0,_marker);
string_delete(response,0,_marker);
var _marker = string_pos(",", response);
}
else
{
responseScript[i] = string_copy(response,0, string_length(response));
}
}
if (keyboard_check_pressed(vk_space))
{
counter++;
}
Then to print in oRespond
/// text
//response
NineSliceBoxStretched(sTextBox, x1,y1,x2,y2, 0);
draw_set_font(fText);
draw_set_halign(fa_center);
draw_set_valign(fa_top);
draw_set_color(c_black);
if (counter % 2 == 0)
{
var _i = 0;
var _print = string_copy(text,1,textProgress);
draw_text((x1+x2) / 2, y1 + 8, _print);
draw_set_color(c_white);
draw_text((x1+x2) / 2, y1 + 7, _print);
_i++;
}
else
{
if (array_length_1d(responseScript) > 0)
{
var _print = "";
for (var t = 0; t < array_length_1d(responseScript); t++)
{
_print += "\n";
if (t == responseSelected) _print += "--> "
_print += responseScript[t];
show_debug_message(responseScript[t]);
if (t == responseSelected) _print += " <-- "
}
draw_text((x1+x2) / 2, y1 + 8, _print);
draw_set_color(c_white);
draw_text((x1+x2) / 2, y1 + 7, _print);
}
}
Alright, i think to see many problems with your code.
First of all, since arrays in GM are dynamic declare them like
line_array[3]
is a bad practice (in my point of view)
I've never declared an array this way in GM so that could be the problem here.
Second, i don't really understand the logic of your code, always create objects, at least in the GM environment, that corresponds to "physical" entities, i would make an object for the Civilian but not for the "respond".
I've red your code a lot of times and since no one answered you in 3 months i can assume it's because no one can really understand your way of coding, and this way of coding will probably give you a lot of problems in future. The thing that you're trying to doing could be super-easy if done with a good hierarchy.
I would like to help u with this code, but i find it very chaotic.
If you've not resolved this problems, write a comment :)
I advice you to fully re-implement it even if resolved anyway.

OCaml Syntax Error while using Threads

let set n v = Thread.delay 1.0; (n := v) in
let result1 x = set x (!x + 1) in
let result2 x = set x (!x * !x) in
let run i =
let original = 2 in
let r = ref (original) in
let t1 = Thread.create result1 r; let v1 = !r in
let v2 = !(result2 r) ;
Thread.join t1 ;
Printf.printf "number is %n" (v1 + v2) in
let main() =
run 1;
run 2 in
main() ;;
In the above program, I am using threads but could not figure out where the syntax error is. If anyone could help me to figure out what the error is.
This line:
let t1 = Thread.create result1 r; let v1 = !r in
Has two lets but only one in. I think you need another in here.
As a side comment, this is an unidiomatic way to structure your code at the top level of a file. It would be more usual to have a series of top-level definitions, which use let but not in. So it would look something like this:
let set n v = Thread.delay 1.0; (n := v)
let result1 x = set x (!x + 1)
let result2 x = set x (!x * !x)
let run i =
let original = 2 in
let r = ref (original) in
let t1 = Thread.create result1 r in
let v1 = !r in
let v2 = !(result2 r) in
Thread.join t1;
Printf.printf "number is %n" (v1 + v2)
let main () = run 1; run 2
let () = main ()
(I haven't compiled this so there might be a few syntax errors, apologies in advance. Just trying to show the basic layout.)

QUIZ: 3 Variables, only 1 if-else to print the difference

I got question. How is it possible to solve a problem where I can only use 1 if-else statement to check 3 variables which one of them is different and print this?
Example:
A = 10
B = 3
C = 10
Answer: B
You didn't specify a language, so this is kinda pseudocode:
if (A == B) {
echo 'C';
} elseif (A == C) {
echo 'B';
} else {
echo 'A';
}
If A and B are equal, we know the different one is C.
Otherwise, if A and C are equal, the different one is B.
C and B aren't the problem? We're only left with A.
In C language it would like this:
int A = 10;
int B = 5;
int C = 10;
if ( A == B) // Check A and B for Equality
printf ("Answer: C");
else if (A == C) // Check A and C for Equality
printf ("Answer: B");
else // Then the answer must be A
printf ("Answer: A");
I found a answer to this! Also I forgot to mention, you're only allowed to use 1 if and else (So no elseifs).
if (A == B) {
print C;
}
else{
print A + B - C;
}

What's the most efficient way to access 2D seismic data

Can anyone tell me the most efficient/performant method to access 2D seismic data using Ocean?
For example, if I need to perform a calculation using data from 3x2D seismic lines (all with the same geometry) is this the most efficient way?
for (int j = 0; j < seismicLine1.NumSamplesJK.I; j++)
{
ITrace trace1 = seismicLine1.GetTrace(j);
ITrace trace2 = seismicLine2.GetTrace(j);
ITrace trace3 = seismicLine3.GetTrace(j);
for (int k = 0; k < seismicLine1.NumSamplesJK.J; k++)
{
double sum = trace1[k] + trace2[k] + trace3[k];
}
}
Thanks
A followup to #Keith's suggestion - with .NET4 his code could be refactored to a generic:
public static IEnumerable<Tuple<T1, T2, T3>> TuplesFrom<T1,T2,T3>(IEnumerable<T1> s1, IEnumerable<T2> s2, IEnumerable<T3> s3)
{
bool m1, m2, m3; // "more" flags
using (var e1 = s1.GetEnumerator())
using (var e2 = s2.GetEnumerator())
using (var e3 = s3.GetEnumerator())
while ((m1 = e1.MoveNext()) &&
(m2 = e2.MoveNext()) &&
(m3 = e3.MoveNext()))
yield return Tuple.Create(e1.Current, e2.Current, e3.Current);
if (m1 || m2 || m3)
throw new ArgumentException(); // sequences of unequal lengths
}
Which gives:
foreach (var traceTuple in TuplesFrom(seismicLine1.Traces, seismicLine2.Traces, seismicLine3.Traces))
for (int k = 0; k < maxK; ++k)
sum = traceTuple.Item1[k] + traceTuple.Item2[k] + traceTuple.Item3[k];
What you have will work except for the two bugs I see, but it can also be made slightly faster. First the bugs. Your loops should be testing NumSamplesIJK.J not .I for the outer loop and .K, not .J for the inner loop. The .I is always 0 for 2D lines.
You can get a slight performance lift by minimizing the dereference of the NumSamplesIJK properties. Since the geometries are the same you should create a pair of variables for the J and K properties and use them.
int maxJ = seismicLine1.NumSamplesIJK.J;
int maxK = seismicLine1.NumsamplesIJK.K;
for (int j = 0; j < maxJ; j++)
...
for (int k = 0; k < maxK; k++)
...
You might also consider using the Traces enumerator instead of calling GetTrace. It will process the data in trace ascending order. Unfortunatley with three lines the code is a bit harder to read.
int maxK = SeismicLine1.NumSamplesIJK.K;
IEnumerator line2Traces = seismicLine2.Traces.GetEnumerator();
ITrace line2Trace = line2Traces.MoveNext();
IEnumerator line3Traces = seismicLine3.Traces.GetEnumerator();
ITrace line3Trace = line3Traces.MoveNext();
foreach (ITrace line1Trace in seismicLine1.Traces)
{
for (int k = 0; k < maxK; k++)
{
double sum = line1Trace[k] + line2Trace[k] + line3Trace[k];
}
line2Trace = line2Traces.MoveNext();
line3Trace = line3Traces.MoveNext();
}
I don't know what, if any, performance lift this might provide. You'll have to profile it to find out.
Good luck.

Optimizing division/exponential calculation

I've inherited a Visual Studio/VB.Net numerical simulation project that has a likely inefficient calculation. Profiling indicates that the function is called a lot (1 million times plus) and spends about 50% of the overall calculation within this function. Here is the problematic portion
Result = (A * (E ^ C)) / (D ^ C * B) (where A-C are local double variables and D & E global double variables)
Result is then compared to a threshold which might have additional improvements as well, but I'll leave them another day
any thoughts or help would be appreciated
Steve
The exponent operator (Math.Pow) isn't very fast, there is no dedicated CPU instruction for calculating it. You mentioned that D and E are global variables. That offers a glimmer of hope to get it faster, if you can isolate their changes. Rewriting the equation using logarithms:
log(r) = log((a x e^c) / (b x d^c))
= log(a x e^c) - log (b x d^c)
= log(a) + log(e^c) - log(b) - log(d^c)
= log(a) + c*log(e) - log(b) - c*log(d)
= log(a) - log(b) + c x (log(e) - log(d))
result = exp(r)
Which provides this function to calculate the result:
Function calculate(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double, ByVal e As Double) As Double
Dim logRes = Math.Log(a) - Math.Log(b) + c * (Math.Log(e) - Math.Log(d))
Return Math.Exp(logRes)
End Function
I timed it with the StopWatch class, it is exactly as fast as your original expression. Not a coincidence of course. You'll get ahead by somehow being able to pre-calculate the Math.Log(e) - Math.Log(d) term.
One easy speed up is that
Result = (A/B) * (E/D)^C
At least you are doing one less exponent.
Depending on what C is, there might be faster ways. Like if C is a small integer.
edit:
adding proof to show this is faster
public static void main(String[] args) {
StopWatch sw = new StopWatch();
float e = 1.123F;
float d = 4.456F;
float c = 453;
sw.start();
int max = 5000;
double result = 0;
for (int a = 1; a < max; a++) {
for (float b = 1; b < max; b++) {
result = (a * (Math.pow(e, c))) / (Math.pow(d, c) * b);
}
}
sw.split();
System.out.println("slow: " + sw.getSplitTime() + " result: " + result);
sw.stop();
sw.reset();
sw.start();
result = 0;
for (int a = 1; a < max; a++) {
for (float b = 1; b < max; b++) {
result = a / b * Math.pow(e/d, c);
}
}
sw.split();
System.out.println("fast: " + sw.getSplitTime() + " result: " + result);
sw.stop();
sw.reset();
}
This is the output
slow: 26062 result: 7.077390271736578E-272
fast: 12661 result: 7.077392136525382E-272
There is some skew in the numbers. I would think that the faster version is more exact (but that's just a feeling since i can't think of exactly why).
Well done for profiling. I would also check that A-C are different on every call. In other words, is it possible the caller is actually calculating the same value over and over again? If so, change it so it caches the answer.
For Math.Floor() function, visit:
http://bitsbyta.blogspot.com/2010/12/math-floor-function-vbnet.html
All functions of math library in vb.net is available at:
http://www.bitsbyta.blogspot.com/