HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Draw crosshairs on the screen - ZedGraph

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecrosshairsdrawzedgraphscreen

Problem

Here's some part of my code is use to draw objects on the screen :

Class members:

private double cursorX = 0;
private double cursorY = 0;
private LineObj cursorOldY = new LineObj();
private LineObj cursorOldX = new LineObj();
private int index1 = 0;

private double cursor2X = 0;
private double cursor2Y = 0;
private LineObj cursor2OldY = new LineObj();
private LineObj cursor2OldX = new LineObj();
private int index2 = -1;

ZedGraph.CurveItem curve = null;


Drawing function :

```
private void drawCursors(bool first, bool second, bool fromKeyboard = false, Point mousePoint = new Point())
{
GraphPane p = lineGraphControl1.GraphPane;
if (p.CurveList.Count == 0 || curve == null)
return;

ZedGraph.Axis axis = curve.IsY2Axis ? (Axis)p.Y2AxisList[curve.YAxisIndex] : (Axis)p.YAxisList[curve.YAxisIndex];
ZedGraph.PointPair point = new PointPair(0.0, 0.0);
if (first)
{
p.GraphObjList.Remove(cursorOldY);
p.GraphObjList.Remove(cursorOldX);
if (fromKeyboard)
point = curve.Points[index1];
else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
{
ZedGraph.CurveItem outputCurve;
p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
if (index1 >= 0)
point = curve.Points[index1];
}
else
{
if (!mousePoint.IsEmpty)
p.ReverseTransform(mousePoint, out cursorX, out cursorY);
point = findPoint(curve, cursorX);
}
cursorX = point.X;
cursorY = transform(point.Y, axis);
cursorOldY = new LineObj(cursorX, p.YAxis.Scale.Min, cursorX, p.YAxis.Scale.Max);
cursorOldX = new LineObj(p.XAxis.Scale.Max, cursorY, p.XAxis.Scale.Min, cursorY);

if (cursorX > p.XAxis.Scale.Min && cursorX p.YAxis.Scale.Min && cursorY = 0)
point = curve.Points[index2];
}
else
{
if (!mousePoint.IsEmpty)

Solution

Mis-Matching curly braces are not a good thing, you do it all over the place, it makes your code hard to read

if (fromKeyboard)
       point = curve.Points[index1];
    else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
    {
        ZedGraph.CurveItem outputCurve;
        p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
        if (index1 >= 0)
           point = curve.Points[index1];
    }
    else
    {


it should look like this

if (fromKeyboard)
   {
       point = curve.Points[index1];
   }
   else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
   {
        ZedGraph.CurveItem outputCurve;
        p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
        if (index1 >= 0)
           point = curve.Points[index1];
   }
   else
   {


Indentation only on an if statement all by itself is fine, I personally don't like it, but when an else statement is added to the if statement, it starts to muddy the waters around your if statement and make it look sloppy.

The single, one-line if statements should also have a buffer zone after them as well.

Code Snippets

if (fromKeyboard)
       point = curve.Points[index1];
    else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
    {
        ZedGraph.CurveItem outputCurve;
        p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
        if (index1 >= 0)
           point = curve.Points[index1];
    }
    else
    {
if (fromKeyboard)
   {
       point = curve.Points[index1];
   }
   else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
   {
        ZedGraph.CurveItem outputCurve;
        p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
        if (index1 >= 0)
           point = curve.Points[index1];
   }
   else
   {

Context

StackExchange Code Review Q#67564, answer score: 5

Revisions (0)

No revisions yet.