How is it possible with gnuplot to plot both isolated points and lines for the same input file?
I mean, once I have a file data.dat of this kind that defines two lines 1-2 and 3-4
x1 y1
x2 y2
x3 y3
x4 y4
I can plot the lines with
$> plot 'data.dat' w lp
but if I want to also add some isolated points to be displayed with gnuplot I would like to add to my data.dat file the following
x1 y1
x2 y2
x3 y3
x4 y4
x5 y5
x6 y6
x7 y7
obviously the points 5,6,7 are treated by gnuplot as points of a line. How can I draw 5,6,7 as isolated points?
Organize your data in blocks. There should be exactly one empty line between two blocks:
x1 y1
x2 y2
x3 y3
x4 y4
x5 y5
x6 y6
x7 y7
Use every to specify which blocks of the datafile should be plotted. The syntax of every is described here or in gnuplot (type help every)
In your case you can then do the following
plot "data.txt" every :::0::1 with lp, "" every :::2::2 with points
if it's acceptable to plot point symbols for points 1..4, too, then just add empty lines after points 5 and 6 and say
plot "1.dat" with lines, "" with points
if not, I'd suggest writing the isolated points to a separate file.
Related
I'm on windows, and I'll work only with windows.
I have a question about opening big files (PTX).
On each line, I will have the coordinates of a points X Y Z I {R G B} ({R, G, B} are not forced to be present).
Since my files are huge (sometimes > 100Go), I would like to read them fastly using memory map (I never did that before), or at least read chunck of memory instead of reading it line by line.
My question is : if I read chunck of memory using
ifstream bigFile("mybigfile.dat");
constexpr size_t bufferSize = 1024 * 1024;
unique_ptr<char[]> buffer(new char[bufferSize]);
while (bigFile)
{
bigFile.read(buffer.get(), bufferSize);
// process data in buffer
}
for example, is there a way to be sure that my buffer won't stop in the middle of a line?
For example, my files is
x1 y1 z1 i1 r1 g1 b1
x2 y2 z2 i2 r2 g2 b2
x3 y3 z3 i3 r3 g3 b3
x4 y4 z4 i4 r4 g4 b4
x5 y5 z5 i5 r5 g5 b5
and I want to create a std::vector<Point>. So I read a buffer size of this file, put it in the buffer, and then I take data from buffer to create my points. But how can I be sure that the buffer won't stop at r3?
If the buffer contains x1 y1 z1 i1 r1 g1 b1 x2 y2 z2 i2 r2 g2 b2 x3 y3 z3 i3 r3 I can't create a point using only x3, y3, z3, i3, r3. I would need g3 and b3 too.
Is there a way to take care of that? I hope that it is understandable, English isn't my native language and I'm not sure I explained it well...
I want to compute a variable X=x1+x2+x3. x1, x2 and x3 have either the values 1 or 0. I recoded system missings to -77. There are some conditions which should be met.
1) If there is a missing value in either x1,x2 or x3, then it should be ignored if one or two of the other variables have the value 1. So the sum should be calculated although there is a missing value but only if there is at least one 1 (Eg. X = x1 + x2 + x3 = 0 + missing + 1 = 1)
2) If there is a missing value in either x1, x2 or x3, then it should not be ignored if there is no 1 at all and the sum should not be calculated. (Eg. X = x1 + x2 + x3 = 0 + missing + missing = missing).
I tried to make a loop with IF but it won't work and I just can't figure out why.
COMPUTE X =SUM(x1, x2, x3).
IF (x1=-77 AND x2~=1 AND x3~=1) X=999.
IF (x2=-77 AND x1~=1 AND x3~=1) X=999.
IF (x3=-77 AND x1~=1 AND x2 ~=1)X=999.
EXECUTE.
These are the returned results:
when x1=1, x2 = 0, x3=-77 then X=1. (That is the result I want.
The problem arises when x1=-77, x2=0, x3=0 because then X=0 and not 999 as I want it to be.
I think that with the loop above I am close to the result but something is missing.
Below I post some other loops I made, but neither did work and I think the one above is the closest to the right answer.
Thank you so much for your help and happy easter!
Cheers desperate Ichav :)
COMPUTE X = x1 + x2 + x3.
RECODE X (SYSMIS=-77).
IF ((X = -77 AND x1 = 1) OR (X = -77 AND x2 = 1) OR (X = -77 AND x3 = 1)) X =1.
EXECUTE.
Here X is always returned as -77.
This is to create some sample data to work on:
data list list/x1 x2 x3.
begin data
1 0 -77
0 -77 0
0 0 0
1 0 1
end data.
MISSING VALUES x1 x2 x3 (-77).
As you can see this is assuming -77 was defined as a missing value - otherwise calculating sum(x1, x2, x3) will fail.
COMPUTE X=SUM(x1, x2, x3).
if X=0 and nmiss(x1, x2, x3)>0 X=999.
Now - if there were no 1 values, the sum is 0. If the sum is zero and there were any (more than 0) missing values involved - the sum is changed to 999.
If you somehow calculated X indirectly without turning -77 into a missing value, you would be able to use the value -77 in if statements (as you tried before). An easier way to do it then:
if X=0 and any(-77, x1, x2, x3) X=999.
I'm a little bit confused about a result that I got after a coefficients reduction on a constraint of a linear programming problem.
The problem is:
maximize z = x1 + x2 + x3 + x4 + x5 + x6
subject to: 6*x1 + 3*x2 - 5*x3 + 2*x4 + 7*x5 - 4*x6 <= 15
where:
1<=x1<=2 continuos
1<=x2<=2 continuos
1<=x3<=2 continuos
1<=x4<=2 continuos
1<=x5<=2 continuos
1<=x6<=2 continuos
After the coefficients reduction the contraints will be:
subject to: 3*x1 + 3*x2 - 3*x3 + 2*x4 + 3*x5 - 3*x6 <= 8
as stated in the Applied Integer Programming book (Der-San Chen - Robert G.Batson - Yu Dang) at page 96 (there is a little error at page 97. The x1 coefficient is 3 not 1).
After that I've tried to submit the problem to ampl with and without the coefficients reduction. But I got two different results:
[without coefficients reduction]
CPLEX 12.6.1.0: optimal integer solution; objective 11.57142857
display x;
x1 2
x2 2
x3 2
x4 2
x5 1.57
x6 2
[with coefficients reduction]
CPLEX 12.6.1.0: optimal integer solution; objective 11.33333333
display x;
x1 2
x2 2
x3 2
x4 2
x5 1.33
x6 2
why? can the solution be considered correct anyway even if the result for x5 is a little different?
I've used three different solver (minos, gurobi, cplex) but they output the same results on the problem.
If you are referring to the technique in 4.4.3, then it's clear what's the problem here.
Suppose we are given a constraint of the form
a1*y1+ a2*y2 + ... + ai*yi < b
where yi = 0 or 1
You are not allowed to use this technique, as your coefficients are continuous ( in [1,2]) and not binary as needed here!
I have start and end coordinate of a line. I want to drawn another line sticking at the end of this this such that they will be perpendicular to each other.
I am trying to do this using the normal geometry. Is there any high-level API there in MFC for the same.
Thanks
If (dx,dy) are the differences in the x and y coodinates of the given line, you can make another line perpendicular by contriving for the differences in its coordinates to be (-dy, dx). You can scale that by any factor (-c*dy, c*dx) to change its length.
You have an existing line (x1, y1) to (x2, y2). The perpendicular line is (a1, b1) to (a2, b2), and centered on (x2, y2).
xdif = x2 - x1
ydif = y2 - y1
a1 = x2 - ydif / 2
b1 = y2 + xdif / 2
a2 = x2 + ydif / 2
b2 = y2 - xdif / 2
I think that works... I tested it for a few lines.
So if you have a line going from (1,1) to (5,3), the perpendicular line would be (5 - 2/2, 3+4/2) to (5 + 2/2, 3 - 4/2) or (4,5) to (6, 1).
You could use SetWorldTransform function from Win32 GDI API.
Sample code is here.
Let me add some c++ code based on kbelder answer. It make one vertex by origin point (x1,y1) and another vertex (x2,y2)
float GetDistance(float x1, float y1, float x2, float y2)
{
float cx = x2 - x1;
float cy = y2 - y1;
float flen = sqrtf((float)(cx*cx + cy*cy));
return flen;
}
void GetAxePoint(double x1, double y1, double x2, double y2, double& x3, double& y3, double vec_len, bool second_is_y)
{
double xdif = x2 - x1;
double ydif = y2 - y1;
if(second_is_y)
{
x3 = x1 - ydif;
y3 = y1 + xdif;
}
else
{
x3 = x1 + ydif;
y3 = y1 - xdif;
}
double vec3_len = GetDistance(x3, y3, x1, y1);
x3 = (x3-x1)/vec3_len;
y3 = (y3-y1)/vec3_len;
x3 = x1 + x3*vec_len;
y3 = y1 + y3*vec_len;
}
I am working in c++ application. And its new to me. Here I write a function which gain two coordinates of a line. I have to process these coordinates and find another point which in that same line.
I will gain A(x1,y1) and B(x2,y2) coordinates.
need to find
C(x3,y3) coordinates.
Therefore I calculate the Slope of given line.
Double slope = (x1-x2)/(y1-y2);
And I know the distance of 3rd point from A point.
Double dis = sqrt(pow(x2-x1) + pow(y2-y1)) * 1.35 ;
I want to find new coordinates x3 ,y3 using Slope and dis.
Can anyone help me to solve this please.
To calculate the x3 I can use mathematical part,
x3 = slope * y3 -------------------1
dis = sqrt(pow(x3-x1) + pow(y3-y1)) ------------2
using these 2 equations which generating in run time , I want to calculate x3 and y3.
Too much math.
x3 = (x1 - x2) * 1.35 + x2
y3 = (y1 - y2) * 1.35 + y2
Unless you are working with a "1.5d" graph y=y(x) you should never use formulas based on y=m*x+q because that doesn't work for vertical lines (and works poorly for near-vertical lines).
In your case the best approach is to use the parametric equation for a line
x = x1 + t * dx
y = y1 + t * dy
where dx = x2 - x1 and dy = y2 - y1 are proportional to the components of the direction unit vector oriented from P1 to P2 and are used instead of m and q to define the line (avoiding any problem with vertical or almost vertical lines).
If you need a point on a specific distance then you just need to find the actual unit vector components with
double dx = x2 - x1;
double dy = y2 - y1;
double dist = sqrt(dx*dx + dy*dy);
dx /= dist;
dy /= dist;
and then the coordinates of the point you need are
double x3 = x1 + prescribed_distance * dx;
double y3 = y1 + prescribed_distance * dy;
or using -prescribed_distance instead depending on which side you want the point: toward P2 or away from it?
If however the prescribed distance is proportional to the current distance between the two points the normalization is not needed and the result can be the simpler:
double x3 = x1 + (x2 - x1) * k;
double y3 = y1 + (y2 - y1) * k;
where k is the ratio between the prescribed distance and the distance between the two points (once again with positive or negative sign depending on which side you are interested in).
By using parametric equations x=x(t), y=y(t) instead of explicit equations y=y(x) in addition to not having artificial singularity problems that depend on the coordinate system you also get formulas that are trivial to extend in higher dimensions. For example for a 3d line you just basically need to add z coordinate to the above formulas in the very same way x and y are used...
If you substitute the first equation "y3 = slope * x3" into the 2nd equation "dis = sqrt(pow(x3-x1) + pow(y3-y1))", and square both sides, you get a quadratic which you can solve using the quadratic formula.
After substitution you get:
dis^2 = (x3-x1)^2 + (slope*x3 - y1)^2
Square both sides:
(slope^2+1)*x3^2 + (-2*slope*y1-2*x1) + 2*y1^2 = dis^2
Solve for x3 using the quadratic formula:
x3 = (2*slope*y1+2*x1) +/- sqrt((2*slope*y1+2*x1)^b - 4*(slope^2+1)*(2*y1^2-dis^2))/(2*(slope^2+1))
Substitute x3 into the first equation to get y3:
y3 = slope * x3