Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't translate properly TextLayout Vector on OnRender()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Can't translate properly TextLayout Vector on OnRender()

    Morning.

    I'm trying to nest a text to a manual hand moving line (in the center of the line), but when i'm trying to translate / move the text on code i'm getting unexpected results on screen.

    What I'm doing first is getting the angle of the line and then rotate the text. After, I reset the rotation.

    My actual code is:

    Code:
    SharpDX.Direct2D1.Brush textBrushDx;
    textBrushDx = textBrush.ToDxBrush(RenderTarget);
    NinjaTrader.Gui.Tools.SimpleFont simpleFont = chartControl.Properties.LabelFont ?? new NinjaTrader.Gui.Tools.SimpleFont("Arial", 16);
    SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();
    SharpDX.DirectWrite.TextFormat textFormat2 = Font.ToDirectWriteTextFormat();
    SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory, text, textFormat2, 400, textFormat1.FontSize);
    textFormat1.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center;
    textFormat2.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center;
    double offsetX = 0;
    double offsetY = 0;
    Point textPoint = new Point();
    textPoint.X = ((startPointAdjusted.X + endPointAdjusted.X) / 2);
    textPoint.Y = ((startPointAdjusted.Y + endPointAdjusted.Y) / 2);                
    
    // Rotate and draw the text              
    
    float anguloLineaRecta = (float)GetAngle(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
    
    SharpDX.Vector2 puntoTextoCentrado = new SharpDX.Vector2((float)(textPoint.X + offsetX), (float)(textPoint.Y + offsetY));
    
    RenderTarget.Transform = Matrix3x2.Rotation((float)(Math.PI * anguloLineaRecta) / 180, puntoTextoCentrado);
    RenderTarget.DrawTextLayout(puntoTextoCentrado, textLayout, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
    RenderTarget.Transform = Matrix3x2.Identity;
    I tried multiply when rotating with the intention of moving text properly (I saw this on some code but i don't understand it).

    Code:
    RenderTarget.Transform = Matrix3x2.Rotation((float)(Math.PI * anguloLineaRecta) / 180, puntoTextoCentrado) * Matrix3x2.Translation(textLayout.Metrics.Width / 2, textLayout.Metrics.Height / 2);

    I uploaded 3 images: Text without moving its position, when i tried translate and what i want.

    Thanks.

    Click image for larger version

Name:	WithoutMoving.png
Views:	64
Size:	15.6 KB
ID:	1304740
    Click image for larger version

Name:	Tranlating.png
Views:	64
Size:	8.4 KB
ID:	1304741
    What I want
    Caption
    Attached Files
    Last edited by franjcy; 05-23-2024, 02:32 PM.

    #2
    Hello franjcy,

    This would require some advanced calculations and custom logic.

    This thread will remain open for any community members that would like to write these calculations on your behalf.

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello franjcy,

      This would require some advanced calculations and custom logic.

      This thread will remain open for any community members that would like to write these calculations on your behalf.

      You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
      Thanks for your answer.

      I'd like a solution of any could give me. I think i'm very close to the solution.

      Or any example where someone do this "calculations" or vector operations.

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello franjcy,

        This would require some advanced calculations and custom logic.

        This thread will remain open for any community members that would like to write these calculations on your behalf.

        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
        I solved this, I'm attaching the code to some needs it:

        Code:
                        // Calcular punto medio de la línea
                        SharpDX.Vector2 midPoint = new Vector2((startVec.X + endVec.X) / 2, (startVec.Y + endVec.Y) / 2);  //vector perpendicular ?
        
                        // Calcular ángulo y  rotación
                        SharpDX.Vector2 delta = new Vector2(endVec.X - startVec.X, endVec.Y - startVec.Y);
                        double angle = Math.Atan2(delta.Y, delta.X); //ángulo para rotar el texto de manera que sea perpendicular a la línea.
                        SharpDX.Vector2 normalVector; //es perpendicular a la línea
                        if (TextoEncimaDeLinea)
                        {
                            normalVector = new Vector2(delta.Y, -delta.X); //el vector de desplazamiento se calcula como
                        }
                        else
                        {
                            normalVector = new Vector2(-delta.Y, delta.X);
                        }            
                        normalVector = Vector2.Normalize(normalVector);
                        SharpDX.Vector2 offset = normalVector * SeparacionTextoLinea;
                        SharpDX.Vector2 textPosition = midPoint + offset;
        
                        // Adjust text position to center the text around textPosition              
        
                        SharpDX.Vector2 adjustedTextPosition = new Vector2(textPosition.X - textLayout.Metrics.Width / 2, textPosition.Y);
        
        
                        // Rotate and draw text
                      
                        RenderTarget.Transform = Matrix3x2.Rotation((float)angle, textPosition); ;
                        RenderTarget.DrawTextLayout(adjustedTextPosition, textLayout, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                        RenderTarget.Transform = Matrix3x2.Identity; // Reset transformation
        
        
                        // Cleanup
                        textFormat1.Dispose();
                        textFormat2.Dispose();  
                        textLayout.Dispose();
                        textBrushDx.Dispose();
                        // Rotate the RenderTarget back
                        RenderTarget.Transform = Matrix3x2.Identity;​

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        579 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        334 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        554 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X